Open In App

Printing structure variables in console in Golang

Last Updated : 17 May, 2020
Improve
Improve
Like Article
Like
Save
Share
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:




// 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:




// 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}


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. C/C++ Code package main import ("fmt") // Fields: structure to b
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 or different data types and is declared by compos
2 min read
Golang - Environment Variables
An Environment Variable is a dynamic object pair on the Operating System. These value pairs can be manipulated with the help of the operating system. These value pairs can be used to store file path, user profile, authentication keys, execution mode, etc. In Golang, we can use the os package to read and write environment variables. 1. Set an enviro
2 min read
Structure Equality 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. This concept is generally compared with the classes in object-oriented programming. It can be termed as a lightw
3 min read
Anonymous Structure and Field 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. Anonymous Structure In Go language, you are allowed to create an anonymous structure. An anonymous structure is
3 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 field of another structure is known as Nested Stru
3 min read
Function as a Field in Golang Structure
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. As we know that in Go language function is also a user-defined type so, you are allowed to create a function fie
2 min read
Promoted Fields in Golang Structure
In Go structure, promoted fields are just like anonymous fields, the type of the field is the name of the field. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like the Anonymous Field to the nested structure. And th
2 min read
Promoted Methods in Golang Structure
In Go structure, the working of promoted methods is just like Promoted Fields. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like the Anonymous Field to the nested structure. And the methods of that structure (other
3 min read
Checking if structure is empty or not in Golang
If the structure is empty means that there is no field present inside that particular structure. In Golang, the size of an empty structure is zero. Whenever the user wants to know if the created structure is empty or not, he can access the structure in the main function through a variable. If there doesn't exist any field inside the structure, he c
3 min read
Article Tags :