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
Recommended Posts:
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to use Field Tags in the Definition of Struct Type in Golang?
- Golang Program to Use Field Tags in the Definition of Struct Type
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- How to add a method to struct type in Golang?
- Pointer to a Struct in Golang
- Printing Struct Variables in Golang
- How to find the type of Struct in Golang?
- How to instantiate Struct Pointer Address Operator in Golang?
- How to instantiate Struct using new keyword in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- How to print struct variables data in Golang?
- Golang | Deadlock and Default Case in Select Statement
- Anonymous Structure and Field in Golang
- Function as a Field in Golang Structure
- reflect.Field() Function in Golang with Examples
- How to set the default value for an HTML <select> element?
- How to set a default parameter value for a JavaScript function ?
- How to get the value of text input field using JavaScript?
- How to detect value change on hidden input field in JQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

