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

@ThunderCat 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"

@Dan 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

9 Answered Questions

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

  • 2013-06-25 19:56:20
  • user387049
  • 83043 View
  • 124 Score
  • 9 Answer
  • Tags:   json go

1 Answered Questions

[SOLVED] Having trouble understanding interface/struct relationship

  • 2015-04-27 03:32:32
  • RockyMountainHigh
  • 468 View
  • 2 Score
  • 1 Answer
  • Tags:   struct go

1 Answered Questions

[SOLVED] how to convert java annotation to go code?

  • 2015-09-15 14:01:54
  • Sandeep Jaluthairya
  • 1031 View
  • 2 Score
  • 1 Answer
  • Tags:   java go

2 Answered Questions

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

  • 2016-04-07 01:20:48
  • MonkeyButter
  • 2824 View
  • 4 Score
  • 2 Answer
  • Tags:   json go

2 Answered Questions

[SOLVED] Go String after variable declaration

  • 2013-09-08 20:56:43
  • YankeeWhiskey
  • 227 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

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

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

1 Answered Questions

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

3 Answered Questions

[SOLVED] golang another func change map of a struct

  • 2016-11-10 04:43:27
  • user3819305
  • 465 View
  • 0 Score
  • 3 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Golang Struct Won't Marshal to JSON

Sponsored Content