golang

Golang : anonymous struct and empty struct

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: http://play.golang.org/p/vhaKi5uVmm package main import "fmt" var battle = make ( chan string ) func warrior ( name string , done chan struct {}) { select { case opponent := <- battle : fmt . Printf ( "%s beat %s\n" , name , opponent ) case battle <- name : // I lost :-( } done <- struct {}{} } func main () { done := make ( chan struct {}) langs := [] string { "Go" , "C" , "C++" , "Java" , "Perl" , "Python" } for _ , l := range langs { go warrior ( l , done ) } for _ = range langs { <- done } } [1st Question] done <- struct {}{}

Go/Golang write log to file

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to write to a log file with Golang. I have tried several approaches, all of which have failed. This is what I have tried: func TestLogging(t *testing.T) { if !FileExists("logfile") { CreateFile("logfile") } f, err := os.Open("logfile") if err != nil { t.Fatalf("error: %v", err) } // attempt #1 log.SetOutput(io.MultiWriter(os.Stderr, f)) log.Println("hello, logfile") // attempt #2 log.SetOutput(io.Writer(f)) log.Println("hello, logfile") // attempt #3 log.SetOutput(f) log.Println("hello, logfile") } func FileExists(name string)

golang POST data using the Content-Type multipart/form-data

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to upload images from my computer to a website using go. Usually, I use a bash script who send file a key to the serveur: curl -F "image"=@"IMAGEFILE" -F "key"="KEY" URL it's work fine, but I'm trying to convert this request into my golang program. http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/ I tried this link and many other. But for each code that I try, the response for the server is "no image sended". And I've no idea why. If someone know what's happend with the exemple above. Thanks 回答1:

Golang append an item to a slice

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Why does a remain the same? Does append() generate a new slice? package main import ( "fmt" ) var a = make ([] int , 7 , 8 ) func Test ( slice [] int ) { slice = append ( slice , 100 ) fmt . Println ( slice ) } func main () { for i := 0 ; i < 7 ; i ++ { a [ i ] = i } Test ( a ) fmt . Println ( a ) } 回答1: In your example the slice argument of the Test function receives a copy of the variable a in the caller's scope. Since a slice variable holds a "slice descriptor" which merely references an underlying array, in your Test function

Assigning value in Golang

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I create a var of type var RespData [] ResponseData ResponseData is a structure as below: type ResponseData struct { DataType string Component string ParameterName string ParameterValue string TableValue *[] Rows } type TabRow struct { ColName string ColValue string ColDataType string } type Rows *[] TabRow I want to fill TableValue of type *[]Rows . Can you please tell me with an example by assigning any values in the TableValue . 回答1: TableValue is a pointer to []Rows (slice of Rows ). Rows is a pointer to a []TabRow (slice of

Difference between golang pointers

匿名 (未验证) 提交于 2019-12-03 02:35:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: There are 2 kinds of variables that I have. Check for the Go playground , and I don't understand why this is happening. The problem: what I get from the Models it should be a struct to use it for GORM First() function. The code: package main import ( "fmt" ) type Test struct { Test string } var Models = map[string]interface{}{ "test": newTest(), } func main() { test1 := Test{} fmt.Println("Test 1: ") fmt.Printf("%v", test1) fmt.Println() fmt.Println("Test 1 as pointer: ") fmt.Printf("%v", &test1) fmt.Println() test2 := Models["test"] fmt

Golang: Having trouble with nested JSON Unmarshaler

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Given the following code: package main import ( "encoding/json" "log" ) type Somefin string func (s *Somefin) UnmarshalJSON(b []byte) error { log.Println("Unmarshaling",string(b)) *s = Somefin("~"+string(b)+"~") return nil } type Wat struct { A, B string *Somefin } func main() { b := []byte(`{"A":"foo","B":"bar","Somefin":"baz"}`) w := &Wat{Somefin: new(Somefin)} err := json.Unmarshal(b,w) log.Println(w, err) } I get the following output: # go run wat.go 2013/12/14 13:59:17 Unmarshaling {"A":"foo","B":"bar","Somefin":"baz"} 2013/12/14 13:59

golang unmarshal complex json

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following JSON blob, and I'm trying to decode it into Go. [ "contig" , "32" , { "a" :[ 33 , 41 , 35 ], "b" :[ 44 , 34 , 42 ]}] I believe that I have to model the data structure of the JSON. I tried using a struct called Line : package main import ( "encoding/json" "fmt" ) type Line struct { Contig string Base string PopMap map [ string ][] int } func main () { j := [] byte ( `["contig", "32", {"a":[33,41,35], "b":[44,34,42]}]` ) var dat Line err := json . Unmarshal ( j , & dat ) fmt . Println ( dat ) fmt . Println ( err

golang - marshal PKCS8 private key?

匿名 (未验证) 提交于 2019-12-03 02:26:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a way to marshal a PKCS8 private key in go 1.5? e.g. something similar to or starting from x509.MarshalPKCS1PrivateKey ? 回答1: Funny enough, there are no standard function to do that, but here is a custom solution: type pkcs8Key struct { Version int PrivateKeyAlgorithm []asn1.ObjectIdentifier PrivateKey []byte } func rsa2pkcs8(key *rsa.PrivateKey) ([]byte, error) { var pkey pkcs8Key pkey.Version = 0 pkey.PrivateKeyAlgorithm = make([]asn1.ObjectIdentifier, 1) pkey.PrivateKeyAlgorithm[0] = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1,

Golang time.Now() is always 2009-11-10 23:00:00 +0000 UTC

匿名 (未验证) 提交于 2019-12-03 02:24:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: im running Go version 1.3 on windows 7x64, after run the following code i always get 2009-11-10 23:00:00 +0000 UTC package main import ( "fmt" "time" ) func main() { fmt.Println(time.Now()) } i know that go playgroud have this fixed time for one reason, but i don't understand why i get this date in my box UPDATE: I realized this issue by upgrading the go version from 1.2.2 to 1.3 Trying to reproduce the issue, i realized this had been fixed after switching off and on computer next day. I recommend to restart the computer after upgrading to 1