草庐IT

pointer_traits

全部标签

pointers - 为什么将 Win32 API 系统调用中使用的缓冲区强制转换为 [1<<20]<type> 数组?

我正在编写一个与WindowsServices交互的golang应用程序使用windows/svc包。当我查看包源代码时,系统调用是如何完成的,我看到了有趣的转换结构:name:=syscall.UTF16ToString((*[1摘自mgr.go这是处理Win32API时的一种常见模式,当需要传递预分配的缓冲区以从Win32API函数接收值(通常是数组或结构)时。我知道WinAPI返回一个由其指针表示的unicode字符串,并将其传递给syscall.UTF16ToString(s[]uint16)在这种情况下,函数将其转换为go字符串。我对将不安全指针转换为指向1M数组的指针的部分

pointers - 通过传递指针更改 slice

我有一个我想使用函数更改的slice(例如,我想删除第一个元素)。我想用一个指针,但我仍然无法索引它。我做错了什么?Playgroundlink:funcchange(list*[]int){fmt.Println(*list)*list=*list[1:]//Thislinescrewseverythingup}varlist=[]int{1,2,3}funcmain(){change(&list)} 最佳答案 您需要使用(*list)。funcchange(list*[]int){*list=(*list)[1:]}或通常更惯用

pointers - 通过传递指针更改 slice

我有一个我想使用函数更改的slice(例如,我想删除第一个元素)。我想用一个指针,但我仍然无法索引它。我做错了什么?Playgroundlink:funcchange(list*[]int){fmt.Println(*list)*list=*list[1:]//Thislinescrewseverythingup}varlist=[]int{1,2,3}funcmain(){change(&list)} 最佳答案 您需要使用(*list)。funcchange(list*[]int){*list=(*list)[1:]}或通常更惯用

pointers - 戈朗 : How to append pointer to slice to slice?

我是一个Golang新手,但我认为我已经掌握了指针和引用的要点,但显然不是:我有一个必须返回[]github.Repository的方法,这是来自Github客户端的类型。API调用返回分页结果,因此我必须循环直到没有更多结果,并将每次调用的结果添加到allRepos变量,然后返回that。到目前为止,这是我所拥有的:func(s*inmemService)GetWatchedRepos(ctxcontext.Context,usernamestring)([]github.Repository,error){s.mtx.RLock()defers.mtx.RUnlock()opt:=

pointers - 戈朗 : How to append pointer to slice to slice?

我是一个Golang新手,但我认为我已经掌握了指针和引用的要点,但显然不是:我有一个必须返回[]github.Repository的方法,这是来自Github客户端的类型。API调用返回分页结果,因此我必须循环直到没有更多结果,并将每次调用的结果添加到allRepos变量,然后返回that。到目前为止,这是我所拥有的:func(s*inmemService)GetWatchedRepos(ctxcontext.Context,usernamestring)([]github.Repository,error){s.mtx.RLock()defers.mtx.RUnlock()opt:=

pointers - 如何定义接收指针的 Go 接口(interface)方法的实现?

下面的程序运行良好。packagemainimport("fmt")typePersoninterface{Hello()}typeJokerstruct{Namestring}func(jJoker)Hello(){fmt.Println(j.Name,"says,\"Hello!\"")}funcmain(){varjJoker=Joker{"Peter"}invokeHello(j)}funcinvokeHello(pPerson){p.Hello()}这是输出。$gorunfoo.goPetersays,"Hello!"但是,当我更改Hello方法以接收指针时,出现错误。pac

pointers - 如何定义接收指针的 Go 接口(interface)方法的实现?

下面的程序运行良好。packagemainimport("fmt")typePersoninterface{Hello()}typeJokerstruct{Namestring}func(jJoker)Hello(){fmt.Println(j.Name,"says,\"Hello!\"")}funcmain(){varjJoker=Joker{"Peter"}invokeHello(j)}funcinvokeHello(pPerson){p.Hello()}这是输出。$gorunfoo.goPetersays,"Hello!"但是,当我更改Hello方法以接收指针时,出现错误。pac

pointers - 通过结构指针定义结构

我不明白为什么在用结构指针(&s)定义结构(sp)后,初始结构(s)在改变后者(sp)的同时不断被修改。http://play.golang.org/p/TdcL_QJqfBtypepersonstruct{namestringageint}funcmain(){s:=person{name:"Sean",age:50}fmt.Printf("%p:%g\n",&s,s.age)sp:=&sfmt.Printf("%p:%g\n",&sp,sp.age)sp.age=51fmt.Printf("%p:%g\n",&sp,sp.age)//yield51fmt.Printf("%p:%g

pointers - 通过结构指针定义结构

我不明白为什么在用结构指针(&s)定义结构(sp)后,初始结构(s)在改变后者(sp)的同时不断被修改。http://play.golang.org/p/TdcL_QJqfBtypepersonstruct{namestringageint}funcmain(){s:=person{name:"Sean",age:50}fmt.Printf("%p:%g\n",&s,s.age)sp:=&sfmt.Printf("%p:%g\n",&sp,sp.age)sp.age=51fmt.Printf("%p:%g\n",&sp,sp.age)//yield51fmt.Printf("%p:%g

转到 RPC 错误 : reading body gob: attempt to decode into a non-pointer

当我调用RPC时,会出现这个错误。而在服务器端,我可以成功接到电话。 最佳答案 错误定义在https://golang.org/src/encoding/gob/decoder.go正如错误所说,解码器需要一个指针。错误的rpc调用是call(address,name,args,reply)。服务器可以成功接收调用,但无法回复,rpc调用失败。正确的方法是call(address,name,args,&reply) 关于转到RPC错误:readingbodygob:attempttode