Printing structure variables in console in Golang Last Updated : 17 May, 2020 Comments Improve Suggest changes Like Article Like Report 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-oriented programming. It can be termed as a lightweight class that does not support inheritance but supports composition. There are two ways to print struct variables on the console as follows: 1. Using the Printf function with the following tags %v the value in a default format %+v the plus flag adds field names %#v a Go-syntax representation of the value Example: C // Golang program to show how to print // the struct variables in console package main // importing package for printing import ( "fmt" ) // taking a struct type emp struct { name string emp_id int salary int } func main() { // an instance of emp (in 'e') e := emp{"GFG", 134, 30000} // printing with variable name fmt.Printf("%+v", e) fmt.Println() // printing without variable name fmt.Printf("%v", e) } Output: {name:GFG emp_id:134 salary:30000} {GFG 134 30000} 2. Print using Marshal of package encoding/json. Example: C // Golang program to show how to print // the struct variables in console package main import ( "encoding/json" "fmt" ) // taking a struct type emp struct { Name string Emp_id string Salary int } func main() { // an instance of emp (in 'e') var e = emp{ Name: "GFG", Emp_id: "123", Salary: 78979854 , } // Marshalling the structure // For now ignoring error // but you should handle // the error in above function jsonF, _ := json.Marshal(e) // typecasting byte array to string fmt.Println(string(jsonF)) } Output: {"Name":"GFG","Emp_id":"123","Salary":78979854} Comment More infoAdvertise with us Next Article Printing structure variables in console in Golang N nikki2398 Follow Improve Article Tags : Go Language Golang-Program Similar Reads Printing Struct Variables in Golang 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 ("f 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 How to fetch an Integer variable as String in GoLang? To fetch an Integer variable as String, Go provides strconv package which has methods that return a string representation of int variable. There is nothing like an integer variable is converted to a string variable, rather, you have to store integer value as a string in a string variable. Following 2 min read Naming the Return Values of a Function in Golang A return value helps to retain the final output of the function after it performs the instructions given in its body. Functions in Golang exhibit variety in return values and rely on the programmer to decide whether to name them or not. Golang introduces a concept of "Naked Return" allowing the use 3 min read Different Ways to Convert an Integer Variable to String in Golang Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function: The Itoa stands for Integer t 3 min read Like