go

Passing a collection of interface types to a function

青春壹個敷衍的年華 提交于 2021-02-08 06:36:39
问题 I'm having trouble figuring out the correct way to use interfaces in Go. My function needs to take a map of items that implement a certain method. It looks like this: type foo interface { bar() string } func doSomething(items map[string]foo) { } I'm trying to call this function with a type that implements the foo interface. type baz struct { } func (b baz) bar() string { return "hello" } items := map[string]baz{"a": baz{}} doSomething(items) But I get the following error: cannot use items

Passing a collection of interface types to a function

谁说胖子不能爱 提交于 2021-02-08 06:36:35
问题 I'm having trouble figuring out the correct way to use interfaces in Go. My function needs to take a map of items that implement a certain method. It looks like this: type foo interface { bar() string } func doSomething(items map[string]foo) { } I'm trying to call this function with a type that implements the foo interface. type baz struct { } func (b baz) bar() string { return "hello" } items := map[string]baz{"a": baz{}} doSomething(items) But I get the following error: cannot use items

Golang Unmarshal JSON

不打扰是莪最后的温柔 提交于 2021-02-08 06:25:39
问题 My input JSON, has a list of different elements. I have problems with the number of the first element of response. Simplified example: package main import ( "fmt" "log" "encoding/json" ) var j = ` { "response": [ 777, // problem here !!! { "id": 888, "from_id": 999, "to_id": 888, "text": "hello..." }, { "id": 999, "from_id": 888, "to_id": 999, "text": "goodbye..." } ] }` type D struct { Id int `json:"id"` FromId int `json:"from_id"` ToId int `json:"to_id"` Text string `json:"text"` } type R

Unmarshaling XML in Go

旧城冷巷雨未停 提交于 2021-02-08 05:51:41
问题 I have the following xml "file": <pinnacle_line_feed> <PinnacleFeedTime>1439954818555</PinnacleFeedTime> <lastContest>34317132</lastContest> <lastGame>218491030</lastGame> <events> <event> <event_datetimeGMT>2015-08-21 09:50</event_datetimeGMT> <gamenumber>483406220</gamenumber> <sporttype>Aussie Rules</sporttype> <league>AFL</league> <IsLive>No</IsLive> <participants> <participant> <participant_name>Hawthorn Hawks</participant_name> <contestantnum>1251</contestantnum> <rotnum>1251</rotnum>

Unmarshaling XML in Go

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 05:51:38
问题 I have the following xml "file": <pinnacle_line_feed> <PinnacleFeedTime>1439954818555</PinnacleFeedTime> <lastContest>34317132</lastContest> <lastGame>218491030</lastGame> <events> <event> <event_datetimeGMT>2015-08-21 09:50</event_datetimeGMT> <gamenumber>483406220</gamenumber> <sporttype>Aussie Rules</sporttype> <league>AFL</league> <IsLive>No</IsLive> <participants> <participant> <participant_name>Hawthorn Hawks</participant_name> <contestantnum>1251</contestantnum> <rotnum>1251</rotnum>

How to prevent SQL Injection in PostgreSQL JSON/JSONB field?

白昼怎懂夜的黑 提交于 2021-02-08 05:49:13
问题 How can I prevent SQL injection attacks in Go while using "database/sql"? This solves the single value field problem because you can remove the quotes, but I can't do that filtering a JSON/JSONB field, like in the following because the $1 is considered a string: `SELECT * FROM foo WHERE bar @> '{"baz": "$1"}'` The following works but it's prone to SQL Injection: `SELECT * FROM foo WHERE bar @> '{"baz": "` + "qux" + `"}'` How do I solve this? EDITED after @mkopriva's comment: How would I build

Golang get command tty output

青春壹個敷衍的年華 提交于 2021-02-08 05:23:23
问题 I'm using go's exec Run command to get command output, which works great when the command 'Stdout' field is set to os.Stdout , and the error is sent to os.Stderr . I want to display the output and the error output to the console, but I also want my program to see what the output was. I then made my own Writer type that did just that, wrote both to a buffer and printed to the terminal. Here's the problem—some applications change their output to something much less readable by humans when it

Golang get command tty output

社会主义新天地 提交于 2021-02-08 05:23:00
问题 I'm using go's exec Run command to get command output, which works great when the command 'Stdout' field is set to os.Stdout , and the error is sent to os.Stderr . I want to display the output and the error output to the console, but I also want my program to see what the output was. I then made my own Writer type that did just that, wrote both to a buffer and printed to the terminal. Here's the problem—some applications change their output to something much less readable by humans when it

SSL options in gocql

萝らか妹 提交于 2021-02-08 05:16:04
问题 In my Cassandra config I have enabled user authentication and connect with cqlsh over ssl. I'm having trouble implementing the same with gocql, following is my code: cluster := gocql.NewCluster("127.0.0.1") cluster.Authenticator = gocql.PasswordAuthenticator{ Username: "myuser", Password: "mypassword", } cluster.SslOpts = &gocql.SslOptions { CertPath: "/path/to/cert.pem", } When I try to connect I get following error: gocql: unable to create session: connectionpool: unable to load X509 key

How to copy from slice to array with seeking onto slice

拟墨画扇 提交于 2021-02-08 03:44:25
问题 I'm writing a library to deal with a binary format. I have a struct with array vars, that I would like to keep for documentation purposes. I need also to seek and tell from the input slice of bytes. Some pseudocode: type foo struct { boo [5]byte coo [3]byte } func main() { // input is a []byte full of datas, read from a file var bar foo // Here i need something that writes 5 bytes to bar.foo from input bar.foo = somefunc(input, numberOfFoo) // ??? // I need also tell() and seek() input.seek(n