Skip to content

Latest commit

 

History

History

006_omitting_empty_fields

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Go Sample Example - Omitting Empty Fields in JSON

This repository demonstrates how to omit empty fields from JSON output in Go using the omitempty struct tag. It showcases how to use Go's encoding/json package to exclude fields that have zero values during serialization.

📖 Information

  • This example covers how to use the `omitempty` tag to exclude empty or zero-value fields from JSON output.
  • It demonstrates defining a struct where certain fields are omitted from the JSON output if they have default values (such as an empty string or zero).

💻 Code Example

package main

import (
	"encoding/json"
	"fmt"
)

type Person6 struct {
	Name    string `json:"name"`
	Age     int    `json:"age,omitempty"`
	Country string `json:"country,omitempty"`
}

func main() {

	// This example shows how to omit empty fields from the JSON output using the omitempty struct tag

	person := Person6{
		Name: "John Doe",
	}

	jsonData, err := json.Marshal(person)
	if err != nil {
		fmt.Println("Error encoding JSON:", err)
		return
	}

	fmt.Println(string(jsonData))
}

🏃 How to Run

  1. Make sure you have Go installed. If not, you can download it from here.

  2. Clone this repository:

    git clone https://github.com/Rapter1990/go_sample_examples.git
  3. Navigate to the 006_omitting_empty_fields directory:

    cd go_sample_examples/030_json/006_omitting_empty_fields
  4. Run the Go program:

    go run 006_omitting_empty_fields.go

📦 Output

When you run the program, you should see the following output:

{"name":"John Doe"}