A struct or structure is a collection of various fields with similar or different data types. Structures are incredibly useful as they allow you to define a layout or blueprint for a specific object in a program. Think of structures as a lightweight version of OOP.

In this guide, we will look at how we can declare a structure and various methods of how to print a struct in Go.

Declaring a Struct

We can declare a struct in go using the keyword. We start with the keyword type, followed by the name of the structure and the keyword struct.

The syntax is shown:

We then include fields of the structure inside a pair of curly braces.

The example below creates a simple structure:

package main

type user struct {


    Name     string


    Age      int


    Employed bool

}


func main() {  

}

In the above syntax, we create a structure called user. We then set the fields of various data types.

Struct Instance

After the structure declaration, we need to create an instance of the structure. The example below shows you how to create an instance of the user struct.

user1 := user{“Jane Doe”, 65, false}

The snippet above creates an instance of the user structure called user1.

Print struct

We can print a struct using the Printf method from the fmt package. It provides us with special formatting options to print a struct. Such options include:

Formatting Option Meaning
%v Print the value of the variable in the default format
% v Print struct field name and its associated value

Consider the example code below:

package main


import “fmt”

type user struct {


    Name     string


    Age      int


    Employed bool

}


func main() {


    user1 := user{“Jane Doe”, 65, false}


    fmt.Printf(“%vn, user1)


    fmt.Printf(“% vn, user1)


    fmt.Printf(“%dn, user1.Age)


    fmt.Printf(“%sn, user1.Name)

}

Using the printf function, we can fetch all fields in the struct or access individual values for the struct.

The resulting output is as shown:

{Jane Doe 65 false}

{Name:Jane Doe Age:65 Employed:false}

65


Jane Doe

Note that the %v formatter prints only the values. If you want to get the field name and the associated value, we use the % v option.

Print Struct – json.Marshall

The second method you can use to print a struct is to use the Marshal() method from the encoding/json package.

Check our tutorial on JSON marshal and Unmarshall in Go to learn more.

The example below illustrates how to use the Marshal function.

package main


import (


    “encoding/json”


    “fmt”

)

type user struct {


    Name     string


    Age      int


    Employed bool

}


func main() {


    user1 := user{“Jane Doe”, 65, false}


    JSON, _ := json.Marshal(user1)


    fmt.Println(string(JSON))

}

The function should return the struct fields and values as shown in the output below:

{“Name”:“Jane Doe”,“Age”:65,“Employed”:false}

Conclusion

The example below discusses various methods to print a struct, such as the Prinft function and JSON marshal.

Thanks for reading!

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/john-150×150.png621352b272b4b.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list