go , Marshalling empty map[string]interface{} results in “null” instead of nil


Marshalling empty map[string]interface{} results in “null” instead of nil

Question:

Tag: go

I have a problem where Go is inserting "null" into my PostgreSQL database's jsonb columns if I try to do the following, and the structs property (in this case of type map[string]interface{}) is empty:

accessMembers, _ := json.Marshal(c.AccessMembers)

Doing a test print it outputs the same as the value which get stored into the database:

fmt.Println(string(accessMembers)) // equals the string "null"

The problem is that I need it to be - nil (not a string, but the Golang nil), so when I use it in the Exec function below:

sqlStr := `UPDATE my_table SET access_members=$1 WHERE id=$2;`

_, err := db.Exec(sqlStr, accessMembers, c.Id)

It should be the equivalent (which works!!) of doing:

_, err := db.Exec(sqlStr, nil, c.Id)

I tried a lot of different workarounds, which I thought had to work, but they didn't.

eg.

var accessMembers []byte

am, _ := json.Marshal(c.AccessMembers)

if string(am) != "null"{
    accessMembers = am
}

sqlStr := `UPDATE my_table SET access_members=$1 WHERE id=$2;`

_, err := db.Exec(sqlStr, accessMembers, c.Id)

Which results in the following error: "pq: invalid input syntax for type json"

I can't understand why this is not the same, as explicitly writing nil in the Exec functions parameter, since the []byte clearly must be nil, right?

What am I doing wrong here? Here's how I had hoped it would work: http://play.golang.org/p/UoLAGfhhRl

Answer:

var accessMembers interface{} = nil

if c.AccessMembers != nil {
    j, _ := json.Marshal(c.AccessMembers)
    accessMembers = j
}

Thanks to thwd for providing the solution basically, and for pointing out how the database driver marshals nil differently, suggesting interface{} instead of []byte.

Thanks to DonSeba for reminding me to check if the struct field exists before using resources to marshal, do string conversion and comparison, which ought to be more expensive.


Answer:

I can't understand why this is not the same, as explicitly writing nil in the Exec functions parameter, since the []byte clearly must be nil, right?

nil has a type. The database driver marshals nil differently for type map[string]interface{} or []byte than for interface{} (which is the type of the second argument of db.Exec). You should be able to do:

var i interface{} = nil

j, _ := json.Marshal(c.AccessMembers)

if string(j) != "null" {
     i = j
}

sqlStr := `UPDATE content SET access_members=$1 WHERE id=$2;`

_, err := db.Exec(sqlStr, i, c.Id)

Related:


'invalid memory address' error with go-mssql


go
I'm having an issue that I can't seem to resolve, probably due to my inexperience with GO. I have the following code working on one server, but not on another. Here is the code: // Build out the connection string to the database, and then open the connection to the...

How do I synchronize a variable number of channels in Go?


go
I am testing out concurrency in Go by writing a client (in the form of a web server) that makes many requests to a web server and then returns how long it took to make those requests. Basically a benchmark tool. Here is to code I am using currently: main.go...

How do I get the submit value from the request object


html,go
I am trying to make a form POST request with two different submit buttons. I would like to try and get the submit button value in go. How do I go about getting this from the http.Request object. HTML Code: <form action="/save" method="POST"> <div><span>Title: </span><textarea name="title" placeholder="Link">{{printf "%s" .Title}}</textarea></div> <div>...

Golang - using/iterating through JSON parsed map


json,parsing,go
With PHP and Javascript (and Node) parsing JSON is a very trivial operation. From the looks of it Go is rather more complicated. Consider the example below package main import ("encoding/json";"fmt") type fileData struct{ tn string size int } type jMapA map[string] string type jMapB map[string] fileData func parseMapA(){ var...

Go Yaml Interpretation Example


go,yaml
This Go Yaml Interpretation Example, https://gist.github.com/suntong001/74c85d15b19ef4b14b0e, can unmarshal a simple YAML like this: foo: 1 bar: - one - two Now I want to interpretation an array of the exact same data structure, what's the correct way to do? Below is my code so far, and it is not working...

Build a go binary for both Linux and Mac [duplicate]


go
This question already has an answer here: Cross compile Go on OSX? 2 answers How can I go build a project in a way that it works for both Mac and Linux (x86 and x64)? Is that even possible? I searched and found some stuff, but cannot make them...

How can I redirect, in Go, the stdout and stderr of a command to both the console and a log file while outputting in real time?


go,io,sync
The following bit of code does exactly what I want, except it only prints to the console. cmd := exec.Command("php", "randomcommand.php") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Fatal(err) } randomcommand.php: // randomcommand.php simply alternates output between stdout and stderr 20 times $stdout...

Getting a PHP configuration variable in Go


php,go
I want to get the output of the command php -r 'echo get_cfg_var("some_var");' to check it against a predefined value. Currently, I have the following code: variableName := "default_mimetype" cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'") out, err := cmd.CombinedOutput() after running, err.Error() returns "exit status 254"...

measure execution time and stop waiting if too long in golang


go
I am trying to measure execution time of funcWithUnpredictiveExecutionTime function. func measureTime(expectedMs float64) (ok bool) { t1 := time.Now() funcWithUnpredictiveExecutionTime() t2 := time.Now() diff := t2.Sub(t1) The measuring is fine when funcWithUnpredictiveExecutionTime works faster than I expected. But if it works slower than expectedMs the measuring will not stop right...

How check error codes with couchbase gocb?


go,couchbase
When handling errors returned by gocb (the official Couchbase Go Client) I would like to check for specific status codes (e.g. StatusKeyNotFound or StatusKeyExists). Like so _, err := bucket.Get(key, entity) if err != nil { if err == gocb.ErrKeyNotFound { ... } } Can this be done?...

How to make this code more performant?


performance,go,hashmap
I have this snippet of code that iterate over a map and filter some fields based on relation type, I have to run two loops and have a feeling that it is going to be slow for big maps. Is there any way I can technique/refactoring to make this code...

Golang ORDER BY issue with MySql


mysql,database,go
I can't seem to dynamically ORDER BY with db.Select(). I've Googled without any luck... WORKS rows, err := db.Query("SELECT * FROM Apps ORDER BY title DESC") DOES NOT WORK rows, err := db.Query("SELECT * FROM Apps ORDER BY ? DESC", "title") I'm not getting any errors, the query simply fails...

Correct use of go context.Context


concurrency,go,gorilla
I just read the article: Build You Own Web Framework In Go and for sharing values among handlers I picked the context.Context and I'm using it in the following way to share values across handlers and middlewares: type appContext struct { db *sql.DB ctx context.Context cancel context.CancelFunc } func (c...

Declare variable with format specifier in Go


go
In Python, I can declare a variable as d ='/some/dir/%s' and later replace %s with any value as >>> d = '/some/dir/%s' >>> d % "hello" '/some/dir/hello' Is it possible to do the same in Go? If so, how?...

Serving Static Files with a HTTP 500 Status


go
Is there a way to serve static files over HTTP in Go with a custom status code (without re-writing a significant amount of private code)? From what I can see: http.ServeFile calls the helper function http.serveFile It then calls http.ServeContent after determining the mod time and size of the file/dir...

Does exist on Go something like macros in C++ like #ifdef so I can choose what to build based on flag?


go
I need to build in go for linux and windows with different packages same file on windows I need to import github.com/hashicorp/go-syslog and on linux import log/syslog and inside code in file I have to use syslog.ALERT or gsyslog.ALERT depending on os. Does exist on Go something like macros in...

How do I save an entire string as a txt file in Go?


string,go
I'm creating a simple word processing program in Go. From the command line, I have two prompts: $Enter Title: $Enter Body: The program is supposed to save the document as a txt file and print it to the command line. The program works if the user user types in a...

Why using naked return and the normal return give me different results?


go,rot13
I'm playing around with Golang tour and I wonder why using naked return give me the correct result but the normal one doesn't. This is the exercise that I have this problem https://tour.golang.org/methods/12. The objective is to create a reader that can decipher rot13. and the rot13 function is already...

Although maps are always reference types, what if they're returned from a non-pointer receiver?


pointers,dictionary,go
Supposedly maps are reference types in Go, so when returning them from functions, you don't need to pass as a pointer to the map in order for the changes to be visible outside the function body. But what if said map is returned from a method on a non-pointer struct?...

How to keep a strong reference in Go?


arrays,go,slice
Is there any way I can keep a strong reference in Go? Given the following convoluted piece of code: package main import ( "fmt" ) func main() { slice := make([]int, 5) slice[3] = 25 // whatever index between 0 and 4 included I don't care slicesArray := make([]*[]int, 2)...

Golang - structs, intializing and memcpy - what is the proper way?


arrays,struct,go,initialization,custom-type
I'm new to Go and I wish to translate some C/C++ code I have to Go but I haven't been able to. The problem resides in 2 places: how can I initialize a struct I've defined and how to do a "memcopy" The code I'm talking about is this: http://play.golang.org/p/e8N255qEAk...

Golang failes to change the name of imported module


go,docker,skydns
I'm trying to bring SkyDNSv1 back to life and build it from my fork (here is Dockerfile). SkyDNS was really good and simple tool for the quick service discovery, but it wasn't updated for a long time. There is an error in build process and it's caused by third party...

Marshalling empty map[string]interface{} results in “null” instead of nil


go
I have a problem where Go is inserting "null" into my PostgreSQL database's jsonb columns if I try to do the following, and the structs property (in this case of type map[string]interface{}) is empty: accessMembers, _ := json.Marshal(c.AccessMembers) Doing a test print it outputs the same as the value which...

CGO converting Xlib XEvent struct to byte array?


c,go,xlib,cgo
I am creating a simple window manager (code based of the c code in tinywm) in Golang. To use Xlib, I am using cgo, so my header is: // #cgo LDFLAGS: -lX11 // #include <X11/Xlib.h> And I have a variable declaration, like: event := C.XEvent{} And then, I use this...

How to serialize a custom formatted time to/from xml in Go?


xml,go,formatting,date-formatting
When serializing a datetime to/from xml how do I make it use a custom time format?

Different performances in Go slices resize


go,stack,benchmarking,slice
I'm spending some time experimenting with Go's internals and I ended up writing my own implementation of a stack using slices. As correctly pointed out by a reddit user in this post and as outlined by another user in this SO answer Go already tries to optimise slices resize. Turns...

Does Mgo cache connection strings?


mongodb,go,mgo,compose
My Go application has been connecting to a MongoDB (hosted on Compose.io) using MGO with no issues. Today I decided to delete this database and add a different one (again using Compose). I updated the connection string. So I connect using: db, err := mgo.Dial("mongodb://<username>:<password>@dogen.mongohq.com:10048/db-name") with the username and password...

panic: runtime error: invalid memory address or nil pointer dereference when running Blockchainr


go,blockchain
I run blockchainr and get the following terminal output: 17:39:57 2015-06-16 [INF] loading db leveldb panic: runtime error: invalid memory address or nil pointer dereference panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x402cb63] goroutine 1 [running]: main.search(0x4911ef8, 0xc20806e2d0, 0x0, 0x0, 0x4911ef8) /Users/mesquka/Downloads/blockchainr-master/src/blockchainr/main.go:185 +0x3fa...

Server initiated requests


http,go,http2
I know that http is a request-response protocol, the problem in short is a client makes a request to the server to start a long running process, and i want to inform the client over the progress with a simple json message containg progress info. in http1 i know that...

Why doesn't “go get gopkg.in/…” work while “go get github.com/…” OK?


windows,git,powershell,github,go
I try to use go get gopkg.in/fatih/pool.v2 to install pool according to Readme.md, but can't success: C:\Users\xiaona\Documents\GitHub> go get -v gopkg.in/fatih/pool.v2 Fetching https://gopkg.in/fatih/pool.v2?go-get=1 https fetch failed. Fetching http://gopkg.in/fatih/pool.v2?go-get=1 import "gopkg.in/fatih/pool.v2": http/https fetch: Get http://gopkg.in/fatih/poo l.v2?go-get=1: dial tcp 107.178.216.236:80: ConnectEx tcp: A connection attempt failed because the connected party did not...

Unmarshal JSON into a map in Go


json,dictionary,go
I'm having trouble figuring out how to load a "subsection" of JSON file into a map element. Background: I'm trying to unmarshal a somewhat complicated configuration file which has a strict structure, so I assume it's preferable to unmarshal into a "static" structure rather than into an interface{}. Here's a...

Handling Custom BSON Marshaling (Golang & mgo)


json,mongodb,go,bson
I have a number of structs that require custom marshalling. When I was testing I was using JSON and the standard JSON marshaller. As it doesn't marshal unexported fields, I needed to write a custom MarshalJSON function, which worked perfectly. When I called json.Marshal on the parent struct containing the...

Trouble retrieving Has Many using Gorm


go,go-gorm
When I try to get episodes back from a podcast I get invalid association []. Not sure what I'm doing wrong. package main import ( "log" "github.com/jinzhu/gorm" _ "github.com/mattn/go-sqlite3" ) type Podcast struct { Id int Title string RssUrl string `sql:"unique_index"` Url string Episodes []Episode } type Episode struct {...

JSON decoding in golang


json,curl,go
So I tried something based on the example here in my code, and get no data, but no error either. The code is: import ( "io" "fmt" "net/http" "encoding/json" ) type Credential struct { username string `json:"username"` password string `json:"password"` } func login(res http.ResponseWriter, req *http.Request) { if req.Method ==...

image.Decode results in “unknown format” when source is multipart.File


image,go,martini
I have a multipart.File that a user uploads to my server, and then I take that file and upload it to s3 using aws-sdk-go, but I also want to create a thumbnail of that image. The code below works fine in my tests when I file is the return value...

Golang switch between structs


struct,go,interface,switch-statement
I'm new to golang and I'm trying to create a function that, based on the struct it's used on, will return a formatted string using Sprintf type Name struct { Title string First string Last string } type Location struct { Street string City string State string Zip string }...

How to change a float64 number to uint64 in a right way?


go,floating-point,type-conversion,floating-point-conversion
package main func main() { var n float64 = 6161047830682206209 println(uint64(n)) } The output will be: 6161047830682206208 It looks like that when float64 change to uint64, the fraction is discarded....

Run Golang as www-data


security,go
When I run a Node HTTP server app I usually call a custom function function runAsWWW() { try { process.setgid('www-data'); process.setuid('www-data'); } catch (err) { console.error('Cowardly refusal to keep the process alive as root.'); process.exit(1); } } from server.listen(8080,'localhost',null,runAsWWW); so the server is actually running as the www-data user to...

Find the shortest path sum in a matrix. Is Dijkstra not optimal for this case?


algorithm,go
I am trying to solve the following problem from project euler (please take a look at description and the example in the link, but here is the short explanation). in the matrix, find the minimal path sum from the top left to the bottom right, by moving left, right, up,...

More strict compile options in GoLang


go
Is there an option in golang to fail build if a function returns a value and the returned value is not used? For example, I want an error / warning when we have: func abc() error and it is called without an lvalue as: abc() EDIT: This is not for...

Referencing yourself inside of a struct


struct,go
Let's say I have a type that's a struct like so: type Authorization struct { Username string Password string Handler func(http.HandlerFunc) http.HandlerFunc } And I have an array of these: type Authorizations map[string]*Authorization I want to be able to do something like this: var auth = Authorizations{ "test": *Authorization{ "someusername",...

Mail with html content shows break lines or ignores newlines


email,templates,go,mandrill
I am sending mail to users via mandrill and I using both smtp and mandrill api to send. Content of the mail is rendered go template (.tpl) When I put template like Hi {{.name}}, <br/> This is support. <br/> it sends via mandrill api ok, but is visible when I...

Cannot call tests inside packages other than main


go
I am writing tests for my go code and inside sender folder, sender package I have added exposed_api_test.go ( also tried exposed_api_test.go because I have code in exposed_api.go) package sender import ( "log" "testing" "github.com/stretchr/testify/assert" ) func TestTimeConsuming(t *testing.T) { assert.Equal(t, "test", "test1") } and when I run build and...

Golang, how to return in func FROM another func?


go,return,func
I want to end execution in parent func apiEndpoint() upon calling/exiting in a child func apiResponse() func apiEndpoint() { if false { apiResponse("error") // I want apiResponse() call to return (end execution) in parent func // so next apiResponse("all good") wont be executed } apiResponse("all good") } func apiResponse(message string)...

goroutine channels over a for loop


json,struct,go,channels
My main function reads json from a file, unmarshals it into a struct, converts it into another struct type and spits out formatted JSON through stdout. I'm trying to implement goroutines and channels to add concurrency to my for loop. func main() { muvMap := map[string]string{"male": "M", "female": "F"} fileA,...

Why does adding parentheses in if condition results in compile error?


syntax,go,syntax-error
The following Go code runs OK: package main import "fmt" func main() { if j := 9; j > 0 { fmt.Println(j) } } But after adding parentheses in condition: package main import "fmt" func main() { if (j := 9; j > 0) { fmt.Println(j) } } There is...

Why won't mgo unmarshall my struct properly?


mongodb,go,bson,mgo
Earlier I posted this question asking about writing custom BSON marshalling/unmarshalling in Go using mgo. Now I've come to test it I think I've hit on a bigger problem. All my structs unmarshal to nil values. This is my currency struct with the implementations of bson.Getter and bson.Setter: type Currency...

convert chan to non chan in golang


go,type-conversion
Is it possible to let function funcWithNonChanResult have the following interface: func funcWithNonChanResult() int { And if I want it to use function funcWithChanResult with the interface: func funcWithChanResult() chan int { In other words, can I somehow convert chan int to int? Or I must have chan int result...

http.newRequest not sending post data


php,go
I have the following code to send post data to a server, but the server is not detecting any post data on the request. Client code: cookieJar, _ := cookiejar.New(nil) client := &http.Client{ Jar: cookieJar, } postUrl := os.Args[1] username := os.Args[2] password := os.Args[3] data := url.Values{} data.Set("username", username)...

Why does my concurrent function exit prematurely in Go?


concurrency,go
I am looking through Go Bootcamp and am reading the Go Concurrency chapter right now. I have never used concurrency before in programming and don't understand the output of this program: package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 2; i++...