草庐IT

成员方

全部标签

go - 如何用指针类型的匿名成员初始化 Go 结构?

用匿名成员初始化结构的正常方法是这样的:packagemainimport"fmt"typeAAstruct{intxxstring}funcmain(){a:=&AA{int:1,xx:"2",}fmt.Println(a)//&{12}}但是,如果类型是指针,就不能再这样做了packagemainimport"fmt"typeAAstruct{*intxxstring}funcmain(){i:=1a:=&AA{*int:&i,xx:"2",}fmt.Println(a)}//.\hello.go:14:invalidfieldname*intinstructinitializer

作为结构成员的函数

我正在尝试使函数成为我的结构中的成员typemyStructstruct{myFunfunc(interface{})interface{}}functestFunc1(bbool)bool{//somefunctionalityhere//returnsabooleanattheend}functestFunc2(sstring)int{//somefunctionalitylikemeasuringthestringlength//returnsanintegerindicatingthelength}funcmain(){fr:=myStruct{testFunc1}gr:=my

go - 为什么 channel 没有成为结构的成员

代码:https://play.golang.org/p/Oh3oTa7GIPXtypeastruct{cchanbool}func(a*a)do(){a.c上面的实际输出是bcDone!预期输出:bcbacDone!我不明白为什么它不打印bac?代码是不言自明的,如果还需要更多细节请询问 最佳答案 你的maingoroutine在b.c上发送一个值,然后等待:b.c您从main启动的goroutine:gob.s()这是从b.c收到的,也来自b.a.c:func(b*b)s(){for{select{case如果从b.c收到一个值

json - 为什么编码 JSON 结构成员不调用自定义 MarshalJSON?

在Golang中,我有一个结构体,其成员是具有常量值的自定义int类型。基本上,自定义类型是一个逻辑枚举。typeFlavorintconst(VanillaFlavor=iotaChocolateStrawberry)func(f*Flavor)MarshalJSON()([]byte,error){return[]byte(strconv.Quote(f.String())),nil}自定义类型定义了MarshalJSON和UnmarshalJSON函数,因此当我将自定义类型序列化为JSON时,我希望在序列化输出中获得值的string,而不是int值。我的问题是,如果我有一个指向包

go - 返回带有数据成员的多态类型

我正在尝试编写一个函数getTargetServer()以返回具有数据成员URL和方法Close()。这将是*Server的概括从httptest.NewServer()返回但我也希望能够返回Close()是NOP的自定义类型。typeexternalTestServerstruct{URLstring}func(externalTestServer)Close(){}funcgetTargetServer()*externalTestServer{ifurlbase,ok:=optionals["urlbase"].(string);ok{return&externalTestSer

javascript - 推送者存在无法使用 angularjs 和 golang 获取成员

到目前为止,我正在尝试使用here中的示例,这就是我的设置//Angular//JSvarpresenceClient=newPusher('API_KEY',{authEndpoint:apiServer+"/presence_auth",authTransport:'jsonp',encrypted:true})varc=pusher.subscribe("presence-testchan")Utils.log(c.members.count)//0Utils.log("000============")c.bind('pusher:subscription_succeeded'

arrays - 函数返回后,在数组的结构成员上设置的值丢失

在golang中,我的理解是arrayslice类型是引用。我遇到了一个问题,golang的行为就像是在复制数据,而不是传递引用。https://play.golang.org/p/EfEOMV_wcStypeTempstruct{Idstring`json:"id"`Loststring`json:"lost"`}funcmakeFoo1()[]Temp{foos:=make([]Temp,0)foos=append(foos,Temp{Id:"foo"})returnfoos}funcmakeFoo2()[]Temp{foos:=makeFoo1()for_,t:=rangefoo

Golang 反射(reflect)在 slice 中获取结构成员

我有以下结构:typeProductionInfostruct{StructA[]struct{Field1stringField2int}我将从ProductionInfo类型的StructA中提取字段名称和类型。但我不明白如何。谁能帮帮我? 最佳答案 使用反射包:f,_:=reflect.TypeOf(ProductionInfo{}).FieldByName("StructA")t:=f.Type.Elem()fori:=0;i 关于Golang反射(reflect)在slice中

unit-testing - 如何在 Golang 中正确模拟具有成员函数的结构?

我有两个结构:FunctionalityClient和TestClient,它们都实现了Interface。我有一个Interface类型的全局变量Client。我将实际客户端或模拟客户端分配给Client,具体取决于它是测试还是正常运行。Interface有一个方法Request我想在测试中模拟它。也就是说,我想:记录传递给函数的参数是什么从函数返回一些任意定义的返回值所以结构看起来像这样:typeTestClientstruct{recordedArgs[]interface{}returnValues[]interface{}}func(c*TestClient)Request(

json - 将 json 请求主体解码为具有自定义接口(interface)类型的结构成员的结构

让我们考虑下面的代码typeAstruct{Column1string`json:"column1"`EntityCustomInterface`json:"entity"`}typeCustomInterfaceinterface{GetType()string}typeEntity1struct{ColumnXstring`json:"columnx"`ColumnYstring`json:"columny"`}typeEntity2struct{ColumnPstring`json:"columnp"`ColumnQstring`json:"columnq"`}func(*eEn