草庐IT

Linux的目录结构

全部标签

go - 结构初始化的嵌套数组 : how to do if constructors for both struct are available?

刚接触golang。我有两种结构类型(称为Inner和Outer),并且每个结构类型都有我想使用的构造函数。外部结构“有一个”内部结构的二维数组。如何在外部结构的构造函数中使用内部类型的构造函数来初始化内部数组?typeInnerstruct{valint}funcnewInner(valint)*Inner{i:=new(Inner)i.val=valreturni}typeOuterstruct{members[][]Innerrowintcolint}funcnewOuter(rowint,colint)*Outer{o:=new(Outer)o.row=rowo.col=col

go - 一个滑行项目应该如何导入另一个带有 vendor/目录的项目?

golangGlide打包器会自动为项目安装依赖项,然而,在这样做时它会从vendor/拉取依赖项,因为许多项目会检查vendor依赖项。应该如何导入包含vendor/目录的glide依赖项?截至目前,您可能会遇到如下错误:./scheduler.go:36:不能使用“github.com/jayunit100/my-project/vendor/github.com/spf13/pflag”.CommandLine(类型*“github.com/jayunit100/my-项目/vendor/a/b/spf13/pflag".FlagSet)作为类型*"github.com/jayu

go - 从字符串列表初始化结构

我正在尝试从字符串列表中初始化结构,但编译器抛出以下错误。我仍在学习这门语言,请原谅我的无知,但这是否可以通过使用类型断言来解决?ERROR:v.UberXundefined(typestringhasnofieldmethodUberX)typeGalaxystruct{UberXint64UberYint64}funcmain(){galaxies:=[]string{"andromeda","milkyway","maffei"}for_,v:=rangegalaxies{v:=&Galaxy{}}for_,v:=rangegalaxies{v.UberX+=1000v.Uber

GO: slice 独特的结构有效的可重用实现

我经常需要根据任意equals函数去除重复项。我需要实现:速度快且内存有效(不创建map)可重用且易于使用,想想slice.Sort()(github.com/bradfitz/slice)不需要保持原slice的顺序或保留原slice最好尽量减少复制这可以在go中实现吗?为什么这个函数不是我所知道的某些库的一部分?我正在寻找例如godash(github.com/zillow/godash)实现使用map并且不允许任意小于和等于。这是大致的样子。测试:import("reflect""testing")typeblastruct{IDstring}typeblas[]blafunc(

go - 为什么 Slices 在传递给 Go 中的函数时内部结构为 "passed by reference"?

packagemainimport"fmt"funcmain(){a:=SomeType{myslice:[]int{1,2,3},decimal:2.33}for_,i:=rangea.myslice{fmt.Println(i)}fmt.Println(a.decimal)addOne(a)for_,i:=rangea.myslice{fmt.Println(i)}fmt.Println(a.decimal)}typeSomeTypestruct{myslice[]intdecimalfloat32}funcaddOne(sSomeType){s.myslice[0]++s.dec

pointers - 在 Go 中修改结构中的结构 slice

在下面的示例中,person有一段friendship,我尝试将一个friendship初始化为指向另一个的指针person对象,但由于某种原因它失败了,结果是没有人有任何friendship。我没有在我应该在的地方使用指针吗?packagemainimport("fmt""math/rand")typefriendshipstruct{friend*person}typepersonstruct{nameintfriendship[]friendship}funccreatePerson(idint)person{returnperson{id,make([]friendship,0

go - 使用反射检索结构变量的注解

在go中,是否可以检索结构的变量注释?考虑以下结构:typeATypestruct{IDstring`xml:"my_id"`Datestring`xml:"creation_ts"`}如何使用反射检索ID字段的xml:"my_id"部分?以下将打印变量的名称、类型和值,但不打印注释。s:=reflect.ValueOf(&aType).Elem()typeOfT:=s.Type()fori:=0;i谢谢, 最佳答案 它作为StructField.Tag可用,所以f.Tag引用资料:reflect.StructFieldreflec

go - 在没有结构的情况下解析 GraphQL 中的列表字段

我有这个GraphQL类型:typeUser{id:Stringname:String}由定义varUserObject=graphql.NewObject(graphql.ObjectConfig{Name:"User",Fields:graphql.Fields{"id":&graphql.Field{Type:graphql.String,},"name":&graphql.Field{Type:graphql.String,},},})在我的根查询中,我想用查询字段users链接一些用户:varRootQuery=graphql.NewObject(graphql.Object

go - 在 golang 中使用 os/exec 在特定目录中执行命令

我想在特定目录中运行一个命令。所以这里有两种方法可以做到这一点。command:=exec.Command("echo*tar.gz|xargs-n1tarzxf")command.Dir=pathFinalcmdErr:=command.Run()另一方面,这对我不起作用,command:="cd"+pathFinal+";"+"echo*tar.gz|xargs-n1tarzxf"cmd:=exec.Command("/bin/sh","-c",command)cmdErr:=command.Run()这是有效的。我想以第一种方式实现它。我不知道为什么它不起作用第二个抛出错误无法解

json - Go:JSON 编码嵌套结构;错误地省略了外部字段

我正在尝试编码嵌套结构。查看一个非功能示例here(我无法在Goplayground中导入“compute”和“pretty”,但我已经重新创建了我的测试逻辑并粘贴了输出)。packagemainimport("encoding/json""fmt""github.com/kylelemons/godebug/pretty"compute"google.golang.org/api/compute/v1")typeCreateInstancestruct{compute.Instance//Additionalmetadatatosetfortheinstance.Metadatama