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.
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
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)
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}
Recommended Posts:
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to print struct variables data in Golang?
- Printing structure variables in console in Golang
- How to add a method to struct type in Golang?
- Pointer to a Struct in Golang
- How to find the type of Struct in Golang?
- How to instantiate Struct Pointer Address Operator in Golang?
- How to use Field Tags in the Definition of Struct Type in Golang?
- How to instantiate Struct using new keyword in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- How to Assign Default Value for Struct Field in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Golang Program to Use Field Tags in the Definition of Struct Type
- Go Variables
- Scope of Variables in Go
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
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.

