Printing Struct Variables in Golang Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Suppose, we need to print the structure with its fields corresponding value. We can do this with the help of package fmt which implements formatted I/O with functions analogous to C's printf and scanf. Let's first try if we just print the structure, what will happen. Go package main import ("fmt") // Fields: structure to be printed type Fields struct { Name string NickName string Age int } func main() { // Initialising the struct with values var f = Fields{ Name: "Abc", NickName: "abc", Age: 19, } // printing the structure fmt.Println(f) } Output: {Abc abc 19} There are two ways to print Struct Variables in Golang. The first way is to use Printf function of package fmt with special tags in the arguments the printing format arguments. Some of those arguments are as below: %v the value in a default format %+v the plus flag adds field names %#v a Go-syntax representation of the value Go package main import ( "fmt" ) // Fields: structure to be printed type Fields struct { Name string NickName string Age int } func main() { // initializing the // struct with values var f = Fields{ Name: "Abc", NickName: "abc", Age: 19, } // printing the structure fmt.Printf("%v\n", f) fmt.Printf("%+v\n", f) fmt.Printf("%#v\n", f) } Output: {Abc abc 19} {Name:Abc NickName:abc Age:19} main.Fields{Name:"Abc", NickName:"abc", Age:19} Printf with #v includes main.Fields that is the structure's name. It includes "main" to distinguish the structure present in different packages. Second possible way is to use function Marshal of package encoding/json. Syntax : func Marshal(v interface{}) ([]byte, error) Go package main import ( "encoding/json" "fmt" ) // Fields: structure to be printed type Fields struct { Name string NickName string Age int } func main() { // initialising the struct // with values var f = Fields{ Name: "Abc", NickName: "abc", Age: 19, } // Marshalling the structure // For now ignoring error // but you should handle // the error in above function jsonF, _ := json.Marshal(f) // typecasting byte array to string fmt.Println(string(jsonF)) } Output: {"Name":"Abc", "NickName":"abc", "Age":19} Comment More infoAdvertise with us Next Article Printing Struct Variables in Golang M manjeet_04 Follow Improve Article Tags : Go Language Similar Reads Printing structure variables in console in Golang A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 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 Nested Structure in Golang A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure. A structure which is the 3 min read Structures in Golang A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 7 min read Pointer to a Struct in Golang In Go, structs are used to create custom data types that group different fields together. When working with structs, using pointers can be especially beneficial for managing memory efficiently and for avoiding unnecessary copying of data. A pointer to a struct allows you to directly reference and mo 3 min read Like