go

Confused about append() behavior on slices

只谈情不闲聊 提交于 2021-02-05 12:12:22
问题 func main() { slice := make([]int, 10, 10) slice[0] = 0 slice[1] = 1 slice1 := slice slice1[0] = 10000 fmt.Println(slice) slice1 = append(slice1, 100) slice1[0] = 20000 fmt.Println(slice) } result: [10000 1 0 0 0 0 0 0 0 0] [10000 1 0 0 0 0 0 0 0 0] In my understanding, slice is a pointer, slice1 and slice point to the same array, and the first output also proves this. But why did slice 's value remain unchanged after the append operation changed slice1 ? 回答1: The append() didn't change

Scan function by reference or by value

邮差的信 提交于 2021-02-05 11:38:23
问题 I have the following code: statement := `SELECT id from source where mgmt = $1 ` var exists string errUnique := dr.db.QueryRow(statement, mgmt).Scan(exists) if errUnique != nil && errUnique != sql.ErrNoRows { return errUnique } The above code works for me. However, shouldn't my .Scan(&exists) have a by reference? This does not work. On the other hand, when I change exists to a bool as seen here var exists bool the .Scan(&exists) suddenly works. Why isn't the string exists and .Scan(&exists)

how to unmarshal json object if object is returning as empty string instead of empty struct

六月ゝ 毕业季﹏ 提交于 2021-02-05 11:37:24
问题 I'm receiving some data as JSON, but if a object is empty, it does not return a empty struct but a empty string instead, and when unmarshaling, it returns an error. So instead of data being {"key":{}} is {"key":""}} , it does not work even using omitempty field Example: https://play.golang.org/p/N1iuWBxuo1C type Store struct { Title string `json:"title,omitempty"` Item item `json:"item,omitempty"` } type item struct { Price float32 `json:"price,omitempty"` Kind string `json:"kind,omitempty"`

Goroutine Timeout

若如初见. 提交于 2021-02-05 11:30:35
问题 type Response struct { data interface{} status bool } func Find() (interface{}, bool) { ch := make(chan Response, 1) go func() { data, status := findCicCode() ch <- Response{data: data, status: status} }() select { case response := <-ch: return response.data, response.status case <-time.After(50 * time.Millisecond): return "Request timed out", false } } So, I have above function. Basically findCicCode() function call makes 3 http calls internally to external services. I have added combined

Placeholders are not replaced

断了今生、忘了曾经 提交于 2021-02-05 10:49:50
问题 I am experimenting with the package http/template . I have also already managed that e.g. the header, footer, navbar and so on were included in the base template: {{ define "base" }} <!DOCTYPE html> <html lang="en"> <!-- Start Head --> <head> {{ template "head" }} </head> <!-- End Head --> <!-- Start Body --> <body> {{ template "navbar" }} {{ template "content" }} {{ template "footer" }} </body> <!-- End Body --> </html> {{ end }} 404 page: {{ define "content" }} [...] <h1 class="text-light

Placeholders are not replaced

怎甘沉沦 提交于 2021-02-05 10:49:47
问题 I am experimenting with the package http/template . I have also already managed that e.g. the header, footer, navbar and so on were included in the base template: {{ define "base" }} <!DOCTYPE html> <html lang="en"> <!-- Start Head --> <head> {{ template "head" }} </head> <!-- End Head --> <!-- Start Body --> <body> {{ template "navbar" }} {{ template "content" }} {{ template "footer" }} </body> <!-- End Body --> </html> {{ end }} 404 page: {{ define "content" }} [...] <h1 class="text-light

Unmarshal JSON Object to struct in Go - Result is empty [duplicate]

血红的双手。 提交于 2021-02-05 10:49:26
问题 This question already has answers here : My structures are not marshalling into json [duplicate] (3 answers) Closed 5 years ago . I'm trying to unmarshal a json object to struct in Go. I tried to stick to this example but I can't get it to work. The result stays empty. Code: package main import ( "encoding/json" "fmt" ) type MyObject struct { id string pubKey string } func main() { x := `{"id":"abc","pubKey":"QIDAQAB"}` fmt.Println("Input: ", x) var myObject MyObject json.Unmarshal([]byte(x),

How do I get the number of rows returned while using database/sql?

纵饮孤独 提交于 2021-02-05 09:45:40
问题 Given the following function: func (me *OrderService) GetOrders(orderTx *sql.Tx, orderId int) (orders *sql.Rows) { orders, err := ecommTx.Query("SELECT * FROM orders WHERE id=?", orderId) if err != nil { log.Fatal(err) } log.Printf("Successfully queried and receive %d orders", orders.count) return orders } Are there any easy ways to .count the results? I'd like to keep this database engine agonistic, but FWIW.... I'm using Matt N's sqlite3 driver for my integration tests, but plan on having a

Deploying a golang app on heroku, build succeed but application error

此生再无相见时 提交于 2021-02-05 08:29:27
问题 My golang app runs on port 9000 at my localhost. After deploying it at heroku using godep support, i was able to push and deploy at heroku. However when i try to access the endpoint e.g '/', it shows Application Error. You can see below my code and log while deploying at heroku package main import ( "log" "fmt" "net/http" "os" "github.com/gorilla/mux" "github.com/gorilla/context" "gopkg.in/paytm/grace.v1" // utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http" // util "bitbucket.org

mongo-go-driver: nested OR/AND query filter

╄→гoц情女王★ 提交于 2021-02-05 08:15:26
问题 I try to create a MongoDB query filter with the nested operators (OR/AND/...). But lib requires to create a bson.D and pass bson.E elements into it. If I need to have OR/AND inside AND/OR - I need to put bson.M + bson.D inside bson.D like this: filter := bson.M{"$and": bson.D{{"p", 10}, bson.M{"$or": bson.D{{"s", 30}, {"a", 1}}}}} .. and of course it doesn't work: cannot use primitive.M literal (type primitive.M) as type primitive.E in slice literal . Probably the same problem will happen if