How to Assign Default Value for Struct Field in Golang? Last Updated : 22 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like 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: C // 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: C // 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 Comment More infoAdvertise with us Next Article How to compare Structs with the Different Values Assigned to Data Fields in Golang? V vanigupta20024 Follow Improve Article Tags : Go Language Golang-Program Similar Reads How to compare Structs with the Different Values Assigned to Data Fields in Golang? A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same 3 min read Function as a Field in Golang Structure In Go, you can define functions as fields within a structure (struct). This feature allows you to associate behaviors (methods) directly with data types, enabling a more organized and encapsulated way of managing data and its related operations.Examplepackage mainimport "fmt"// Define a struct with 2 min read How to print struct variables data in Golang? A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same 2 min read Anonymous Structure and Field in Golang In Golang, structures (or structs) allow us to group elements of various types into a single unit, which is useful for modeling real-world entities. Anonymous structures are unnamed, temporary structures used for a one-time purpose, while anonymous fields allow embedding fields without names.Example 3 min read How to Access Interface Fields in Golang? Go language interfaces are different from other languages. In Go language, the interface is a custom type that is used to specify a set of one or more method signatures and the interface is abstract, so you are not allowed to create an instance of the interface. But you are allowed to create a varia 3 min read How to declare and access pointer variable in Golang? Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always 3 min read Like