Open In App

How to Assign Default Value for Struct Field in Golang?

Improve
Improve
Like Article
Like
Save
Share
Report

Default values can be assigned to a struct by using a constructor function. Rather than creating a structure directly, we can use a constructor to assign custom default values to all or some of its members.

Example 1:




// Golang program to assign
// default values to a struct 
// using constructor function
package main
  
import (
    "fmt"
)
  
// declaring a student struct
type Student struct{
      
    // declaring struct variables
    name string
    marks int64
    age int64
}
  
// constructor function
func(std *Student) fill_defaults(){
  
    // setting default values
    // if no values present
    if std.name == "" {
        std.name = "ABC"
    }
      
    if std.marks == 0 {
        std.marks = 40
    }
      
    if std.age == 0 {
        std.age = 18
    }
}
  
// main function
func main() {
  
    // creating a struct where
    // only the name is initialised
    std1 := Student{name: "Vani"}
      
    // printing the struct 
    // with no default values
    fmt.Println(std1)
      
    // this will assign default values 
    // to non-initialised valiables
    // in struct std1
    std1.fill_defaults()
      
    // printing after assigning
    // defaults to struct variables
    fmt.Println(std1)
      
    // creating a struct where
    // age and marks are initialised
    std2 := Student{age: 19, marks: 78}
      
    // assigning default name
    std2.fill_defaults()
      
    // printing after assigning 
    // default name to struct
    fmt.Println(std2)
      
}


Output:

{Vani 0 0}
{Vani 40 18}
{ABC 78 19}

Another way of assigning default values to structs is by using tags. Tags can only be used for string values only and can be implemented by using single quotes(”).

Example 2:




// Golang program to assign
// default values to a struct 
// using tags
  
package main
  
import (
    "fmt"
    "reflect"
)
  
// declaring a person struct
type Person struct {
  
    // setting the default value 
    // of name to "geek"
      name string `default:"geek"
  
}
  
func default_tag(p Person) string {
  
    // TypeOf returns type of 
    // interface value passed to it
      typ := reflect.TypeOf(p)
  
    // checking if null string
      if p.name == "" {
          
        // returns the struct field 
        // with the given parameter "name"
            f, _ := typ.FieldByName("name")
          
        // returns the value associated 
        // with key in the tag string
        // and returns empty string if 
        // no such key in tag
            p.name = f.Tag.Get("default")
      }
  
      return fmt.Sprintf("%s", p.name)
}
  
// main function
func main(){
  
    // prints out the default name 
    fmt.Println("Default name is:", default_tag(Person{}))
}


Output:

Default name is: geek


Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads