By Doug Knesek


2014-10-12 16:34:12 8 Comments

type TestObject struct {
    kind string `json:"kind"`
    id   string `json:"id, omitempty"`
    name  string `json:"name"`
    email string `json:"email"`
}

func TestCreateSingleItemResponse(t *testing.T) {
    testObject := new(TestObject)
    testObject.kind = "TestObject"
    testObject.id = "f73h5jf8"
    testObject.name = "Yuri Gagarin"
    testObject.email = "[email protected]"

    fmt.Println(testObject)

    b, err := json.Marshal(testObject)

    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(string(b[:]))
}

Here is the output:

[ `go test -test.run="^TestCreateSingleItemResponse$"` | done: 2.195666095s ]
    {TestObject f73h5jf8 Yuri Gagarin [email protected]}
    {}
    PASS

Why is the JSON essentially empty?

2 comments

@Cerise Limón 2014-10-12 16:38:09

You need to export the fields in TestObject by capitalizing the first letter in the field name. Change kind to Kind and so on.

type TestObject struct {
 Kind string `json:"kind"`
 Id   string `json:"id,omitempty"`
 Name  string `json:"name"`
 Email string `json:"email"`
}

The encoding/json package and similar packages ignore unexported fields.

The `json:"..."` strings that follow the field declarations are struct tags. The tags in this struct set the names of the struct's fields when marshaling to and from JSON.

playground

@Damon 2016-07-24 04:48:34

there is supposed to be no "space" before "omitempty"

@Daniel Farrell 2018-02-08 03:25:47

But note below the answer below; this happens automatically if the struct field's first letter is capitalized.

@Sourabh Bhagat 2016-07-19 09:40:43

  • When the first letter is capitalised, the identifier is public to any piece of code that you want to use.
  • When the first letter is lowercase, the identifier is private and could only be accessed within the package it was declared.

Examples

 var aName // private

 var BigBro // public (exported)

 var 123abc // illegal

 func (p *Person) SetEmail(email string) {  // public because SetEmail() function starts with upper case
    p.email = email
 }

 func (p Person) email() string { // private because email() function starts with lower case
    return p.email
 }

@vuhung3990 2017-03-27 07:08:11

awesome man, work perfect only change first letter to UPPER CASE, thank you so much

@Mohsin 2017-06-24 14:56:06

Exactly, In Go, a name is exported if it begins with a capital letter. To put it in context, visit this Go Basics Tour

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Removing fields from struct or hiding them in JSON Response

  • 2013-06-25 19:56:20
  • user387049
  • 99767 View
  • 146 Score
  • 10 Answer
  • Tags:   json go

3 Answered Questions

[SOLVED] Encoding struct to json Go

1 Answered Questions

[SOLVED] Strange behaviour when Unmarshalling into struct in Go

  • 2018-11-08 23:57:32
  • robbieperry22
  • 43 View
  • 1 Score
  • 1 Answer
  • Tags:   go unmarshalling

2 Answered Questions

[SOLVED] Marshaling JSON []byte as strings in Go

  • 2016-04-07 01:20:48
  • prl900
  • 4965 View
  • 6 Score
  • 2 Answer
  • Tags:   json go

0 Answered Questions

Assign additional field when unmarshalling JSON object to GO struct

  • 2018-09-22 21:13:55
  • Hennessy
  • 183 View
  • 0 Score
  • 0 Answer
  • Tags:   json go struct

2 Answered Questions

[SOLVED] Go String after variable declaration

  • 2013-09-08 20:56:43
  • YankeeWhiskey
  • 323 View
  • 0 Score
  • 2 Answer
  • Tags:   syntax go

29 Answered Questions

[SOLVED] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

1 Answered Questions

How do I perform Joinquery with search in MongoDB and go?

  • 2017-09-20 05:30:44
  • Swati
  • 73 View
  • 0 Score
  • 1 Answer
  • Tags:   mongodb go

1 Answered Questions

[SOLVED] How to insert a struct with nil field to mongo?

  • 2017-08-11 14:09:26
  • SokIsKedu
  • 317 View
  • 2 Score
  • 1 Answer
  • Tags:   go struct mgo

1 Answered Questions

[SOLVED] How to create “RegistryAuth” for Private Registry Login Credentials

Sponsored Content