草庐IT

human-interface-guidelines

全部标签

go - interface{} 变量到 []interface{}

我有一个interface{}变量,我知道它是一个指向slice的指针:funcisPointerToSlice(valinterface{})bool{value:=reflect.ValueOf(val)returnvalue.Kind()==reflect.Ptr&&value.Elem().Kind()==reflect.Slice}但我发现很难将其类型转换为[]interface{}变量:ifisPointerToSlice(val){slice,worked:=reflect.ValueOf(val).Elem().Interface().([]interface{})//

go - interface{} 变量到 []interface{}

我有一个interface{}变量,我知道它是一个指向slice的指针:funcisPointerToSlice(valinterface{})bool{value:=reflect.ValueOf(val)returnvalue.Kind()==reflect.Ptr&&value.Elem().Kind()==reflect.Slice}但我发现很难将其类型转换为[]interface{}变量:ifisPointerToSlice(val){slice,worked:=reflect.ValueOf(val).Elem().Interface().([]interface{})//

pointers - 如何获取指向被屏蔽为接口(interface)的变量的指针?

我不想深入探讨以下情况的基本原理。它与解码序列化对象有关,该对象可以是一组固定类型中的任何一种,但您不知道是哪种类型。我有以下类型:typeIinterface{Do()}typesomeIstruct{}func(i*someI)Do(){}typeotherIstruct{}func(i*otherI)Do(){}因此,两个结构其中的指针实现了接口(interface)I。现在我有这个方法想要返回一个I类型的值:funcGetSomeI(marshalled[]byte)(I,error){varobjinterface{}//Thefollowingmethodmagically

pointers - 如何获取指向被屏蔽为接口(interface)的变量的指针?

我不想深入探讨以下情况的基本原理。它与解码序列化对象有关,该对象可以是一组固定类型中的任何一种,但您不知道是哪种类型。我有以下类型:typeIinterface{Do()}typesomeIstruct{}func(i*someI)Do(){}typeotherIstruct{}func(i*otherI)Do(){}因此,两个结构其中的指针实现了接口(interface)I。现在我有这个方法想要返回一个I类型的值:funcGetSomeI(marshalled[]byte)(I,error){varobjinterface{}//Thefollowingmethodmagically

go - 返回它自身的接口(interface)(可克隆)

我正在尝试创建一个可克隆的接口(interface),但在获取结构来实现该接口(interface)时遇到了一些问题。看来这是一个限制,在许多其他语言中都没有。我试图了解此限制的理由。var_Cloneable=test{}typeCloneableinterface{Clone()Cloneable}typeteststruct{}func(t*test)Clone()*test{c:=*treturn&c}Playground:https://play.golang.org/p/Kugatx3Zpw后续问题,因为它对我来说仍然很奇怪。这也不编译var_Cloneable=&test

go - 返回它自身的接口(interface)(可克隆)

我正在尝试创建一个可克隆的接口(interface),但在获取结构来实现该接口(interface)时遇到了一些问题。看来这是一个限制,在许多其他语言中都没有。我试图了解此限制的理由。var_Cloneable=test{}typeCloneableinterface{Clone()Cloneable}typeteststruct{}func(t*test)Clone()*test{c:=*treturn&c}Playground:https://play.golang.org/p/Kugatx3Zpw后续问题,因为它对我来说仍然很奇怪。这也不编译var_Cloneable=&test

go - 如何在 golang 中原子存储和加载接口(interface)?

我想写一些这样的代码:varmyValueinterface{}funcGetMyValue()interface{}{returnatomic.Load(myValue)}funcStoreMyValue(newValueinterface{}){atomic.Store(myValue,newValue)}似乎我可以在原子包中使用LoadUintptr(addr*uintptr)(valuintptr)和StoreUintptr(addr*uintptr,valuintptr)来实现这个,但我不知道如何在uintptr、unsafe.Pointer和interface{}之间转换。

go - 如何在 golang 中原子存储和加载接口(interface)?

我想写一些这样的代码:varmyValueinterface{}funcGetMyValue()interface{}{returnatomic.Load(myValue)}funcStoreMyValue(newValueinterface{}){atomic.Store(myValue,newValue)}似乎我可以在原子包中使用LoadUintptr(addr*uintptr)(valuintptr)和StoreUintptr(addr*uintptr,valuintptr)来实现这个,但我不知道如何在uintptr、unsafe.Pointer和interface{}之间转换。

performance - 当函数参数是接口(interface)时复制什么

假设:在go中,所有函数参数都是按值传递的。为了获得按引用传递的语义/性能,Go程序员通过指针传递值。Go仍然会复制这些参数,但它会复制指针,这有时比复制实际参数的内存效率更高。问题:传递一个接口(interface)是怎么回事?即,在这样的程序中packagemainimport"fmt"typeMessagesstruct{hellostring}funcmain(){sayHelloOne(Messages{"helloworld"});sayHelloTwo(&Messages{"helloworld"});sayHelloThree(Messages{"helloworld"

performance - 当函数参数是接口(interface)时复制什么

假设:在go中,所有函数参数都是按值传递的。为了获得按引用传递的语义/性能,Go程序员通过指针传递值。Go仍然会复制这些参数,但它会复制指针,这有时比复制实际参数的内存效率更高。问题:传递一个接口(interface)是怎么回事?即,在这样的程序中packagemainimport"fmt"typeMessagesstruct{hellostring}funcmain(){sayHelloOne(Messages{"helloworld"});sayHelloTwo(&Messages{"helloworld"});sayHelloThree(Messages{"helloworld"