草庐IT

make_new_type

全部标签

go - 当变量已经声明时,为什么我必须将类型传递给 make 函数?

我想使用匿名结构来做一些事情。varusers[]struct{Namestring`json:"name,omitempty"`Ageint}我必须再次编写类型来设置值users=make([]struct{Namestring`json:"name,omitempty"`Ageint},0,10)如果我坚持使用匿名结构,有没有办法让它更体面?(如果不是,我想知道为什么golang的设计会做出这样的功能...) 最佳答案 匿名结构对于一次性使用非常方便。如果您多次使用它,请定义一个命名结构类型。如果你只在一个函数中使用它,你可以在

go - 不能使用 args (type []string) 作为 type []interface {}

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭3年前。我的golangsqlite插入函数。我正在使用这个包"github.com/mattn/go-sqlite3"funcInsert(args...string)(errerror){db,err:=sql.Open("sqlite3","sqlite.db")iferr!=nil{return}q,err:=db.Prepare(args[0])iferr!=nil{return}_,err=q.Exec(args[1:]...)return}main(){err:=I

go - 在 golang 中,如何将 interface{} 类型断言为 reflect.Type 指定的类型?

例如,我有一个名为a的interface{},还有一个名为elemTypereflect.Type/。现在,我想给elemType键入asserta,但是a.(elemType)无法编译成功。如何解决?对不起我的困惑表达。我的意思是我从一个函数中得到一个类型,我想为这个类型断言一个接口(interface){},但是这个类型存储在一个reflect.Type变量中。我想做的类似于下面的代码:varainterface{}//dosomethingfuncgetType()reflect.Type{varretreflect.Type//dosomethingreturnret}targ

go - 传播时 "Cannot use variable of type []struct as []interface"

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭8个月前。原型(prototype)函数functest(i...interface{}){//Codehere}预期用途typefoostruct{//Fields}foos:=[]foo{//foo1,foo2...}test(foos...)//ERRORtest(foos[1],foos[2],...)//OK错误cannotusefoos(variableoftype[]foos)as[]interface{}valueinargumenttot

go - type <Name> <dataType> 和 type <Name> = <dataType> 有什么区别

我是Golang的新手。抱歉,我仍然对以下两者之间的区别感到困惑:type和type=这是一个例子:packagemainimport"fmt"funcmain(){var(strWordWordstrTextText)strWord="gopher"strText="golang"fmt.Printf("strWord=%s,TypeofValue=%T\n",strWord,strWord)fmt.Printf("strText=%s,TypeofValue=%T\n",strText,strText)}typeWordstringtypeText=string输出strWord=

戈朗 : Wait x amount of time before looping again without starting new goroutine

我有一个循环,需要等待一段随机时间才能再次循环。我有什么:for{rand.Seed(time.Now().UnixNano())r:=rand.Int()//Dostufft,_:=time.ParseDuration(string(r)+"ms")time.Sleep(t)}不幸的是,循环会运行多次,就像time.Sleep不工作一样。 最佳答案 您应该检查当前从t,_:=time.ParseDuration中丢弃的错误:您传递给Sleep的time.Duration处于零值,这会导致函数休眠0纳秒。更改#1:处理错误t,err

go - *[]Type 和 []*Type 在 go 中有什么区别

假设我们有一个名为Person的结构,它由一个名为People的结构持有。typePerson{Namestringageint}typePeople{CitystringList[]*Person//checkthisout}typePeople2{CitystringList*[]Person//What'sthedifference?}[]*Person和*[]Person到底是什么意思?如何获取这些slice的元素值?我比较熟悉C,所以如果你能用C解释一下,我将不胜感激 最佳答案 []*Type是指向Type的指针片段。*[

go - 结构体在 Go 中包含接口(interface)时的 new 与 {}

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。想改善这个问题吗?更新问题,使其成为on-topic对于堆栈溢出。2年前关闭。Improvethisquestion我有一个问题,就像下面的代码一样。packagemaintypeIinterface{Get()}typeAnimalstruct{}func(a*Animal)Get(){}typeDogstruct{Animal}funcmain(){variIi=new(Dog)//successi=Dog{}//error}游乐场:https://play.golang.org/

go - Type.Field 和 Value.Field 有什么区别?

以下代码。funcfieldsTest(targetinterface{})([]field,error){s:=reflect.ValueOf(target)s=s.Elem()targetType:=s.Type()fori:=0;i如果目标接口(interface)是struct,f的返回值和structField一样吗? 最佳答案 Type.Field()返回reflect.StructField类型的值,和Value.Field()返回reflect.Value类型的值.所以它们不能相同。Type.Field()返回描述字

go - Go sync.pool 比 make 慢很多吗?

我尝试使用sync.Pool来重用[]byte。但事实证明它比make慢。代码:packagemainimport("sync""testing")funcBenchmarkMakeStack(b*testing.B){forN:=0;N结果:$gotestpool_test.go-bench=.-benchmemBenchmarkMakeStack-420000000000.29ns/op0B/op0allocs/opBenchmarkBytePool-410000000017.2ns/op0B/op0allocs/op根据Go文档,sync.Pool应该更快,但我的测试显示并非如此