草庐IT

go-simplejson

全部标签

go - 数组中的引用类型

看下面的代码片段//Preparesomedatatoinsertintothetemplate.typeRecipientstruct{Name,GiftstringAttendedbool}varrecipients=[]Recipient{{"AuntMildred","bonechinateaset",true},{"UncleJohn","moleskinpants",false},{"CousinRodney","",false},}我创建了一个具有一些属性的结构。创建Recipient类型的slice后。slice接收者是否在内部保留值或引用类型?好像是值类型。

go - 带有内置网络爬虫的 Martini Go 服务器在几个小时后打开的文件太多

我构建了一个网络爬虫,提供一些有关其发现的http信息。爬虫作为goroutine运行,martini运行web服务器。过了一会儿,我开始得到2014/08/0110:23:51http:Accepterror:accepttcp[::]:3000:toomanyopenfiles;retryingin1s.我读到我应该尝试增加最大打开文件数我只是这个配置级别的新手并且不知道如何做到这一点。我在Ubuntu14.04上运行它。请问如何更改martini服务器的最大打开文件数,谢谢。 最佳答案 确保不要忘记关闭从http.Get获得的

time - 我如何在 Go 语言中格式化时间

我需要这样格式化时间Mon,16Jun201409:19:01+0200代码如下a:=time.RFC1123Z给我Mon,02Jan200615:04:05-0700这似乎是正确的,但我猜需要当前时间并以某种方式使用now()。但还没想出办法。 最佳答案 您需要使用Format为了这。time.RFC1123Z只是一个布局字符串。t:=time.Now()s:=t.Format(time.RFC1123Z)//Formatttoastringusingthegivenlayoutfmt.Println(s)

go - 在 Go 中使用自定义结构作为另一个结构中的类型而不是构建

我有以下目录结构:github.commeeeprojectAfoofoo.gobarbar.go在foo.go中:packagefooimport("github.com/meee/projectA/bar")typeFoostruct{NamestringBars[]Bar}在bar.go中:packagebartypeBarstruct{Namestring}这不会编译/构建,我得到的错误是:undefined:Bar既然导入了,不知道为什么编译不上 最佳答案 如果导入fmt,则不能直接调用Println。您必须改为调用fmt

go - 下面的c.Args() > 0有什么用

此代码来自cliGo包:https://github.com/codegangsta/clipackagemainimport("github.com/codegangsta/cli""os")funcmain(){app:=cli.NewApp()app.Name="greet"app.Usage="fighttheloneliness!"app.Flags=[]cli.Flag{cli.StringFlag{Name:"lang,l",Value:"english",Usage:"languageforthegreeting",},}app.Action=func(c*cli.Co

Go:需要设置属性但没有指针接收器?

packagemainimport"fmt"typeMyClassstruct{datastring}func(thisMyClass)MyMethod(){this.data="Changed!"}funcmain(){obj:=MyClass{}obj.MyMethod()fmt.Println(obj)}我需要通过MyMethod()更改data属性,但我无法将接收器类型更改为指针(func(this*MyClass))因为它必须满足接收者不是指针的接口(interface),是否可以通过其他方式实现? 最佳答案 您需要使用指

go - 结构中的私有(private)/公共(public)领域..表现不同

为什么我可以这样做packagemainimport"fmt"funcmain(){c:=Circle{x:0,y:0,r:5}fmt.Println(c.r)}typeCirclestruct{xfloat64yfloat64rfloat64}http://play.golang.org/p/0ypcekVDV9当我不能对包中的结构执行相同的操作时?如果我尝试访问带有小写字段的结构,则会返回编译器错误。 最佳答案 如前所述,需要导出字段才能从另一个包访问。查看specExportedidentifiersAnidentifierm

go - 来自嵌入式类型的复合文字和字段

我正在编写一个示例程序来回答这里关于SO的另一个问题,发现自己对以下代码无法编译这一事实感到有些困惑;https://play.golang.org/p/wxBGcgfs1opackagemainimport"fmt"typeAstruct{FNamestringLNamestring}typeBstruct{A}func(a*A)Print(){fmt.Println(a.GetName())}func(a*A)GetName()string{returna.FName}func(b*B)GetName()string{returnb.LName}funcmain(){a:=&A{F

go - 是否可以在缓冲区之上写入?

buff:=bytes.NewBuffer(somebytes)如何写在buff之上?目前我正在创建一个新缓冲区。这是正确的方法吗?newBuff:=bytes.NewBuffer(otherbytes)newBuff.ReadFrom(buff) 最佳答案 bytes.NewBuffer()返回*Buffer.*Buffer实现了io.Writer(和io.Reader)所以你可以通过调用它的Write()来简单地写入它或WriteString()方法。例子:somebytes:=[]byte("abc")buff:=bytes.

interface - Go:工厂返回指针和接口(interface){}类型

考虑以下工厂:GoplaygroundtypeTypeAstruct{placeholderint}funcNewTypeA()*TypeA{returnnew(TypeA)}funcmain(){factory:=make(map[string]func()interface{})factory["TypeA"]=NewTypeA}这给了我以下错误:cannotuseNewTypeA(typefunc()*TypeA)astypefunc()interface{}inassignment这很清楚。我认为interface{}可以与指针匹配。我找到了这个解决方案:Goplaygroun