Why struct isn’t getting any data on unmarshal in Go?

Among first few problems, a go lang programmer will probably be struggling with a blank struct. Structs have blank values when one of the following things are wrong:

1. Invalid Naming Conventions:

First letter of fields in struct should be capital. For example


//Invalid name 	
type Person struct {
    name string
    age  int
}

//Valid Name
type Person struct {
    Name string
    Age  int
}

2. Invalid Matches:

Consider this is the json which we can’t change.


{
 FirstName: "John Doe",
 Age: "30",
}

 


//Results in blank data 	
type Person struct {
    FirstName string //This will match first_name but we have FirstName
    Age  int //This will match age but we have Age
}

//The fix
type Person struct {
    FirstName string `json:"FirstName"`
    Age  int `json:"Age"`
}

Tell your network!

First three utility packages for a Go Lang developer

With awesome Go, we have some usual problems which are solved by some awesome packages. Some common problems which every programming language has are – versioning, dependency and debugging.

1. GVM

GVM – Go Version Manager It helps in switching between different go versions. Depending on code base, you can easily switch between different go version.

2. DEP

Dep is dependency manager for go lang. It is production ready. All your codes dependency is maintain in vendor folder. You start with dep init command and it creates a configuration for you.

3. PRETTY

Pretty package helps you debugging variables by printing them in a pretty way.

So before your project goes out of control, it is better to learn and use these three packages right from the start.

Tell your network!