草庐IT

glx_context

全部标签

go - 不止一次取消一个context.Context?

我正在使用Go开发控制台音乐播放器。每当用户选择并播放专辑时,我都会启动一个goroutine以循环播放列表。playlist:=make([]*Media,0)for_,path:=rangealbum.Paths{media,err:=NewMediaFromPath(path)//returnerrplaylist=append(playlist,media)}foridx:=rangeplaylist{player.SetMedia(playlist[idx])err=player.Play()//checkerrstatus,err:=player.MediaState()/

http - 如何将 context.Done() 与嵌套的 http 中间件一起使用

我想知道如何正确实现/使用context.Done()在HTTP中使用它时的方法服务器和实现middleware,我的目标是在客户端跨嵌套中间件断开连接时取消后续事件。为了测试,我创建了以下代码,我不知道这样做是否正确,因为我必须创建一个channel在HandleFunc内和一个goroutine处理请求,将所有这些放在一个select中等待语句。packagemainimport("fmt""log""net/http""time")funchello(whttp.ResponseWriter,r*http.Request){ctx:=r.Context()log.Println(

google-app-engine - App Engine go context.Context 没有命名空间

从ctx,ctxErr:=appengine.Namespace(ctx,"MyContext")获取上下文后,我希望ctx命名空间填充MyContext代码import(..."golang.org/x/net/context""google.golang.org/appengine")ctx:=appengine.NewContext(r)ctx,ctxErr:=appengine.Namespace(ctx,"MyContext")ifctxErr!=nil{log.Errorf(ctx,"Failedtoobtaincustomnamespacecontext,error:%s

google-app-engine - c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log 错误

在GoRuntime中,我使用方法c.Infof来记录消息,但编译失败并出现以下错误c.Infof未定义(类型context.Context没有字段或方法Infof)。错误清楚地表明从c:=appengine.NewContext(r)返回的应用引擎上下文是context.Context类型并且它上面没有方法c.Infof。但与此相反的是https://godoc.org/google.golang.org/appengine/log中的文档表明存在这种方法。还有一点需要注意,该方法存在于“appengine”(导入“appengine”)包返回的上下文中,而这似乎不存在于新包goog

Docker 构建给出 "unable to prepare context: context must be a directory:/Users/tempUser/git/docker/Dockerfile"

我有一个用于构建Ubuntu镜像的Dockerfile。但每当我运行时dockerbuild-tubuntu-test:latest./Dockerfile它在控制台上显示以下错误unabletopreparecontext:contextmustbeadirectory:/Users/tempUser/git/docker/Dockerfile我在MacOsX上。我也尝试过sudo。没有任何效果。 最佳答案 您需要改为指向目录。您不得指定dockerfile。dockerbuild-tubuntu-test:latest.工作。d

Docker 构建给出 "unable to prepare context: context must be a directory:/Users/tempUser/git/docker/Dockerfile"

我有一个用于构建Ubuntu镜像的Dockerfile。但每当我运行时dockerbuild-tubuntu-test:latest./Dockerfile它在控制台上显示以下错误unabletopreparecontext:contextmustbeadirectory:/Users/tempUser/git/docker/Dockerfile我在MacOsX上。我也尝试过sudo。没有任何效果。 最佳答案 您需要改为指向目录。您不得指定dockerfile。dockerbuild-tubuntu-test:latest.工作。d

google-app-engine - Google App Engine 模块主机名 : not an App Engine context

我正在尝试发现AppEngine上的其他已部署服务。类似于this文章建议。我的代码是这样的:import("fmt""net/http""google.golang.org/appengine")funcServiceHostname(serviceNamestring,r*http.Request)(string,error){ctx:=appengine.NewContext(r)hostname,err:=appengine.ModuleHostname(ctx,serviceName,"","")iferr!=nil{return"",fmt.Errorf("unableto

Golang Gin "c.Param undefined (type *gin.Context has no field or method Param)"

我尝试使用作为Golang框架的Gin。https://github.com/gin-gonic/gin我从官方github上复制了示例代码。就像这样。packagemainimport("github.com/gin-gonic/gin""net/http")funcmain(){router:=gin.Default()router.GET("/user/:name",func(c*gin.Context){name:=c.Param("name")c.String(http.StatusOK,"Hello%s",name)})router.Run(":8080")}但是我得到了错

go - 将 context.Context 传递给在单独的 goroutine 中执行的闭包的最佳方法

将context.Context传递给闭包以在单独的goroutine中执行的最佳方法是什么?因为我没有在闭包内改变context.Context,所以我假设这两个选项都是有效的。第二个选项可以通过不复制界面来为我节省一点内存。1)作为参数传递funcrun(ctxcontext.Context){fori:=0;i2)公开外部上下文变量funcrun(ctxcontext.Context){fori:=0;i 最佳答案 两者都应该没问题。要记住的关键是上下文是不可变的。这意味着当您尝试读取上下文时,不存在其他goroutine正在

google-maps - 我应该将新的还是旧的 Context 对象发送到我自己的 gRPC 服务器中的第 3 方 gRPC API?

我们有一个用golang编写的gRPC服务器。对于其中一个RPC,我们从GoogleMapsgRPCAPI请求数据。一旦我们收到来自GoogleMapsRPC的响应,我们就会进行一些计算并将响应返回给封闭的RPC(由我们的服务器定义)。自然地,当在我们的服务器上调用RPC时,我们会收到一个Context对象。我的问题是:我们是否应该将这个相同的Context对象传递给GoogleMapsRPC?或者,我们是否应该创建一个新的Context对象(使用context.Background()),然后将其传递给GoogleMapsAPI? 最佳答案