How to Assign Default Value for Struct Field in Golang? Last Updated : 22 Jun, 2020 Comments Improve Suggest changes 3 Likes 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 Create Quiz Comment V vanigupta20024 Follow 3 Improve V vanigupta20024 Follow 3 Improve Article Tags : Go Language Golang-Program Explore Go Tutorial 3 min read OverviewGo Programming Language (Introduction) 7 min read How to Install Go on Windows? 3 min read How to Install Golang on MacOS? 4 min read Hello World in Golang 3 min read FundamentalsIdentifiers in Go Language 3 min read Go Keywords 2 min read Data Types in Go 7 min read Go Variables 9 min read Constants- Go Language 6 min read Go Operators 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if) 5 min read Loops in Go Language 5 min read Switch Statement in Go 2 min read Functions & MethodsFunctions in Go Language 3 min read Variadic Functions in Go 3 min read Anonymous function in Go Language 2 min read main and init function in Golang 2 min read What is Blank Identifier(underscore) in Golang? 3 min read Defer Keyword in Golang 3 min read Methods in Golang 3 min read StructureStructures in Golang 7 min read Nested Structure in Golang 3 min read Anonymous Structure and Field in Golang 3 min read ArraysArrays in Go 7 min read How to Copy an Array into Another Array in Golang? 3 min read How to pass an Array to a Function in Golang? 2 min read SlicesSlices in Golang 14 min read Slice Composite Literal in Go 3 min read How to sort a slice of ints in Golang? 2 min read How to trim a slice of bytes in Golang? 3 min read How to split a slice of bytes in Golang? 3 min read StringsStrings in Golang 7 min read How to Trim a String in Golang? 2 min read How to Split a String in Golang? 3 min read Different ways to compare Strings in Golang 2 min read PointersPointers in Golang 8 min read Passing Pointers to a Function in Go 3 min read Pointer to a Struct in Golang 3 min read Go Pointer to Pointer (Double Pointer) 4 min read Comparing Pointers in Golang 3 min read Like