go

Strange CSV result for quoted strings in go encoding/csv

流过昼夜 提交于 2021-02-04 18:48:47
问题 I have this little bit of code that kept me busy the whole weekend. package main import ( "encoding/csv" "fmt" "log" "os" ) func main() { f, err := os.Create("./test.csv") if err != nil { log.Fatal("Error: %s", err) } defer f.Close() w := csv.NewWriter(f) var record []string record = append(record, "Unquoted string") s := "Cr@zy text with , and \\ and \" etc" record = append(record, s) fmt.Println(record) w.Write(record) record = make([]string, 0) record = append(record, "Quoted string") s =

Using a variable in the struct tag

不打扰是莪最后的温柔 提交于 2021-02-04 18:41:13
问题 How would I use a variable in a Go struct tag? This works: type Shape struct { Type string `json:"type"` } This does not work: const ( TYPE = "type" ) type Shape struct { Type string fmt.Sprintf("json:\"%s\"", TYPE) } In the first example, I am using a string literal, which works. In the second example, I am building a string using fmt.Sprintf , and I seem to be getting an error: syntax error: unexpected name, expecting } Here it is on the Go playground: https://play.golang.org/p/lUmylztaFg

Correct tile movement for a 2048 game

心不动则不痛 提交于 2021-02-04 18:30:37
问题 I decided to make a 2048 Command Line Edition but I'm having trouble getting the right tile movement... My current structure is that the board is a 2D array (4x4) of ints. When an input is received, it will try to push every tile in that direction (ignoring tiles with value 0), if it notices a change it will start over (Because a tile on the bottom row will have to go all the way up, not just one step up). However, a side effect of this is the following problem: [2][2][4] with the command ->

Correct tile movement for a 2048 game

▼魔方 西西 提交于 2021-02-04 18:30:30
问题 I decided to make a 2048 Command Line Edition but I'm having trouble getting the right tile movement... My current structure is that the board is a 2D array (4x4) of ints. When an input is received, it will try to push every tile in that direction (ignoring tiles with value 0), if it notices a change it will start over (Because a tile on the bottom row will have to go all the way up, not just one step up). However, a side effect of this is the following problem: [2][2][4] with the command ->

盘点Python列表生成式的三种方法

亡梦爱人 提交于 2021-02-04 17:52:37
点击上方“ Go语言进阶学习 ”,进行关注 回复“ Go语言 ”即可获赠从入门到进阶共10本电子书 今 日 鸡 汤 兴来每独往,胜事空自知。 一、前言 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。 二、案例分析 三种方法 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(1, 11))。 print ( list (range( 1 , 11 ))) 如果要生成[1x1, 2x2, 3x3, …, 10x10]怎么做? 1. 方法一是循环: L = [] for x in range ( 1 , 11 ): L. append (x * x) print (L) 但是循环太繁琐,而列表生成式则可以用一行语句代替循环生成上面的list: print ([x * x for x in range(1, 11)]) 写列表生成式时,把要生成的元素x * x放到前面,后面跟for循环,就可以把list创建出来,十分有用,多写几次,很快就可以熟悉这种语法。 for循环后面还可以加上if判断,这样就可以筛选出仅偶数的平方: for x in range ( 1 , 11 ): L. append (x * x) print ([x * x for x in

Compiler: too many arguments given despite that all are given

空扰寡人 提交于 2021-02-04 17:39:08
问题 I want to use the struct DataResponse as parameter for JSON() to respond with the user. By initializing an instance of DataResponse I get the error message, that too many arguments are given, but gave all that are necessary. type DataResponse struct { Status int `json:"status"` Data interface{} `json:"data"` } func GetUser(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) { user := models.User{} // Fetching user from db resp := DataResponse(200, user) JSON(rw, resp) // rw is

Short circuit evaluation in Go

て烟熏妆下的殇ゞ 提交于 2021-02-04 17:28:32
问题 My understanding of short circuit evaluation is that an expression is only called when needed in an if statement. Does Go follow this? For instance, would I get better performance on average from: if !isValidQueryParams(&queries) || r == nil || len(queries) == 0 { return "", fmt.Errorf("invalid querystring") } ...to this: if r == nil || len(queries) == 0 || !isValidQueryParams(&queries) { return "", fmt.Errorf("invalid querystring") } ...since isValidQueryParams is a function with much more

Calling C function that requires sockaddr

穿精又带淫゛_ 提交于 2021-02-04 16:27:09
问题 I have a C function that takes a sockaddr pointer. I've created a syscall.RawSockaddrInet4 object, but I cannot figure out how to cast the address of the object to a C.sockaddr pointer. I've tried this, (*syscall.RawSockaddr)(unsafe.Pointer(&sa)), but I get an error: cannot use (*syscall.RawSockaddr)(unsafe.Pointer(&sa)) (type *syscall.RawSockaddr) as type *C.struct_sockaddr in argument to _Cfunc_lib_connect Any ideas would be much appreciated! int lib_connect( int sock, enum lib_sock_type

why is this a deadlock in golang / waitgroup?

半城伤御伤魂 提交于 2021-02-04 15:29:17
问题 I'm not sure what I'm missing but I get a deadlock error. I'm using a buffered channel that I range over after all go routines complete. The channel has the capacity of 4 and I'm running 4 go routines so I'm expecting it to be "closed" automatically once it reaches the max capacity. package main import "fmt" import "sync" func main() { ch := make(chan []int, 4) var m []int var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() ch <- m return }() } wg.Wait() for c

How to update Mongodb fields with omitempty flag in Golang structure

若如初见. 提交于 2021-02-04 12:38:28
问题 I am working on a Coupon form in which I have some optional fields. Introduction: All the form field values are received as JSON and mapped into a Golang structure. In the structure, I have added an "omitempty" flag with every field. So only those form values are mapped which have some appropriate value, rest of the values like 0, " ", false are ignored by the structure. Here is the Golang structure type Coupon struct { Id int `json:"id,omitempty" bson:"_id,omitempty"` Name string `json:"name