How to print struct variables data in Golang? Last Updated : 22 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 or different data types and is declared by composing a fixed set of unique fields. Syntax: type StructName struct { field1 fieldType1 field2 fieldType2 } struct variables: variable_name := structure_variable_type {value1, value2...valuen} There are two ways to print Struct Variables in Golang. The first way is to use the 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 Example 1: C // Go program to print struct variables data package main import ( "fmt" ) // Creating a structure type Employee struct { Name string Age int Salary int } // Main function func main() { // Creating variables // of Employee structure var e Employee e.Name = "Simran" e.Age = 23 e.Salary = 30000 // Print variable name, value and type fmt.Printf("%s\n", e.Name) fmt.Printf("%d\n", e.Age) fmt.Printf("%d\n", e.Salary) fmt.Printf("%#v\n", e) } Output: Simran 23 30000 main.Employee{Name:"Simran", Age:23, Salary:30000} Example 2: C // Go program to print struct variables data package main import "fmt" type Mobiles struct { company string price int mobile_id int } func main() { // Declare Mobile1 of type Mobile var Mobile1 Mobiles // Declare Mobile2 of type Mobile var Mobile2 Mobiles // Mobile 1 specification Mobile1.company = "Vivo" Mobile1.price = 20000 Mobile1.mobile_id = 1695206 // Mobile 2 specification Mobile2.company = "Motorola" Mobile2.price = 25000 Mobile2.mobile_id = 2625215 // print Mobile1 info fmt.Printf("Mobile 1 company : %s\n", Mobile1.company) fmt.Printf("Mobile 1 price : %d\n", Mobile1.price) fmt.Printf("Mobile 1 mobile_id : %d\n", Mobile1.mobile_id) // print Mobile2 info fmt.Printf("Mobile 2 company : %s\n", Mobile2.company) fmt.Printf("Mobile 2 price : %d\n", Mobile2.price) fmt.Printf("Mobile 2 mobile_id : %d\n", Mobile2.mobile_id) } Output: Mobile 1 company : Vivo Mobile 1 price : 20000 Mobile 1 mobile_id : 1695206 Mobile 2 company : Motorola Mobile 2 price : 25000 Mobile 2 mobile_id : 2625215 To know about this, you can refer to this article. Comment More infoAdvertise with us Next Article How to print struct variables data in Golang? S shivanisinghss2110 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 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 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 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 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 How to use strconv.IsPrint() Function in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides an IsPrint() function which is used to check whether the rune is defined as printable by Go or not with the same definition as unicode.IsPrin 2 min read How to Find Length of Struct in Golang? In programming, data structures are vital in organizing and storing data efficiently. One such data structure in Go, also known as Golang, is the struct. This article will delve into the concept of structure in Golang and explore various methods to find its length.Table of ContentWhat is Struct with 5 min read How to add a method to struct type in Golang? Structs consist of data, but apart from this, structs also tell about the behavior in the form of methods. Methods attached to structs is very much similar to the definition of normal functions, the only variation is that you need to additionally specify its type. A normal function returning an inte 3 min read How to find the type of Struct in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support 3 min read How to Assign Default Value for Struct Field in Golang? 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 f 2 min read Like