草庐IT

alias-method

全部标签

methods - 方法接收器作为指针与否之间的区别

为什么我不必将PrintValue()定义为指针接收器(*One)就能够打印“hello”?packagemainimport"fmt"typeOnestruct{astring}func(o*One)AssignValue(){o.a="hello"}func(oOne)PrintValue(){fmt.Println(o.a)}funcmain(){one:=One{}one.AssignValue()one.PrintValue()} 最佳答案 因为one已经是One类型。实例化语法t:=One{}创建value类型One而表

go - 马提尼绑定(bind) "cannot return value obtained from unexported field or method"

我有以下路线:m.Post("/users",binding.Bind(models.User{}),func(usermodels.User,rrender.Render)当我尝试执行Post请求时收到以下错误消息:"PANIC:reflect.Value.Interface:cannotreturnvalueobtainedfromunexportedfieldormethod"typeUserstruct{idintUUIDstring`json:"uuid"`Usernamestring`json:"userName"form:"userName"binding:"requir

go - RPC 错误 : code = Unimplemented desc = RPC method not implemented

我一直在尝试在Go中创建一个grpc客户端,并且我遵循了官方grpc中显示的正确说明。地点。当我启动用node.js编写的grpc服务器时,连接运行良好,但是在Go中编译ProtocolBuffer并使用正确的grpc客户端配置创建客户端接口(interface)时,我遇到了错误。这是我的identity.pb.go中的内容。typeIdentityServiceClientinterface{CreateUser(ctxcontext.Context,in*GoogleIdToken,opts...grpc.CallOption)(error,*UserInfo)}typesimpl

methods - 如何获取并发方法

如何获取并发方法?typeteststruct{foouint8baruint8}funcNewTest(arg1string)(*test,os.Error){...}func(self*test)Get(strstring)([]byte,os.Error){...}我认为方法Get()的所有代码都应该放在gofunc()的内部,然后使用channel。func(self*test)Get(strstring)([]byte,os.Error){gofunc(){//Codeforthismethod.}()}如果从Get()调用另一个方法会不会有问题?或者它也必须是并发的吗?

go - mgo : result has no field or method 错误

我有以下代码packagemainimport("encoding/json""fmt""labix.org/v2/mgo""labix.org/v2/mgo/bson")funcinsertEntry(j*map[string]interface{},entrystring){err:=json.Unmarshal([]byte(entry),j)iferr!=nil{panic(err)}}funcmain(){c1:=`{"mw":42.0922,"ΔfH°gas":{"value":372.38,"units":"kJ/mol"},"S°gas":{"value":216.81

去基础 : What is the diference between calling a method on struct and calling it on a pointer to that struct?

假设我有一个Vertex类型typeVertexstruct{X,Yfloat64}我已经定义了一个方法func(v*Vertex)Abs()float64{returnmath.Sqrt(v.X*v.X+v.Y*v.Y)}这两个调用有什么区别?(两者返回相同的结果)v1:=Vertex{3,4}fmt.Println(v1.Abs())v2:=&Vertex{3,4}fmt.Println(v2.Abs()) 最佳答案 第一个版本相当于varv1Vertexv1.X=3v1.y=4fmt.Println((&v1).Abs)第二个

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

methods - Go中的参数传递

假设我有一个类型为http.HandleFunc的方法typeHandlerFuncfunc(ResponseWriter,*Request)我想将它包装在另一个相同类型的方法中,有点像这样:funcMyWrapper(reshttp.ResponseWriter,req*http.Request){//dostuffAnotherMethod(res,req)//如果我做对了;当我调用AnotherMethod(res,req)我正在传递res(的值)的副本至AnotherMethod,这意味着该对象现在在内存中复制。有没有办法将指针传递给res至AnotherMethod然后在那里

methods - 难以理解一段 golang 代码

packagemaintypeWriteableinterface{OnWrite()interface{}}typeResultstruct{Messagestring}func(r*Result)OnWrite()interface{}{returnr.Message}//whatdoesthislinemean?whatisthepurpose?var_Writeable=(*Result)(nil)funcmain(){}代码片段中的注释表达了我的困惑。据我了解,带有注释的行通知编译器检查结构是否已实现接口(interface),但我不太确定。谁能帮忙解释一下用途?

methods - 带有方法/函数的 Golang 逗号

关闭。这个问题是opinion-based.它目前不接受答案。想改善这个问题吗?更新问题,以便可以通过editingthispost用事实和引文回答问题.6年前关闭。Improvethisquestion为什么我们允许在方法声明、参数列表或返回列表中的接收器末尾放置逗号。像这样:func(ds*dasa,)yoooooolo(dsadasa,)(dsaadasa,)为什么这是允许的?结尾处都是逗号吗? 最佳答案 Golang允许在许多声明后使用尾随逗号。这可能是语言设计者的一个明确的设计选择,以便更宽容不相关的语法错误;然而,只有他