Golang Structs

In this tutorial, we will learn how to work with Structs in Golang with examples.

Go’s structs are typed collections of fields. They’re useful for grouping data together to form records.

Table of contents

  • Go - struct definition
  • Go - Simple Structs Example
  • Go - anonymous struct example
  • Go - nested structs example
  • Go - struct functions fields example
  • Go - struct constructor example

Go - struct definition

A struct is defined with the type keyword:

type User struct {
    firstName       string
    lastName string
    email        string
}

Go - Simple Structs Example

The following is a Go struct simple example.

package main

import "fmt"

type User struct {
    firstName    string
    lastName     string
    email        string
}

func main() {

    u := User{"John", "Cena", "[email protected]"}

     // print user structure
     fmt.Println(u)

     // The struct fields are accessed with the dot operator.
     fmt.Println(u.firstName)
     fmt.Println(u.lastName)
     fmt.Println(u.email)
}

Output:

{John Cena [email protected]}
John
Cena
[email protected]

Go - anonymous struct example

It is possible to create anonymous structs in Go. Anonymous structs do not have a name. They are created only once.

An anonymous struct is created only with the struct keyword. The declaration of the struct is followed by its initialization.

Here is a simple Golang anonymous struct example:

package main

import "fmt"

func main() {

    u := struct {
        firstName       string
        lastName string
        email        string
    }{
        firstName:      "John",
        lastName: 	"Cena",
        email:        	"[email protected]",
    }

     // The struct fields are accessed with the dot operator.
        fmt.Println(u.firstName)
	fmt.Println(u.lastName)
	fmt.Println(u.email)
}

Output:

John
Cena
[email protected]

Go - nested structs example

Go structs can be nested so, in this example, we show how to create nested structs in Golang with an example.

In the example, the Address struct is nested inside the User struct:

package main

import "fmt"

type Address struct {
    city    string
    country string
}

type User struct {
    firstName    string
    lastName     string
    email        string
    address Address
}

func main() {

    user := User{
        firstName:      "John",
        lastName: 		"Cena",
        email:        	"[email protected]",
	address: Address{
            city:    "Mumbai",
            country: "India",
        },
    }
	
    fmt.Println("firstName:", user.firstName)
    fmt.Println("lastName:", user.lastName)
    fmt.Println("City:", user.address.city)
    fmt.Println("Country:", user.address.country)
}

Output:

firstName: John
lastName: Cena
City: Mumbai
Country: India

Go - Struct functions fields example

In Golang, the Struct fields can be functions so in this example, we show how to define functions as fields in Struct with an example.

In the example, we have the User struct. Its getUserName field is a function called getUserName.

package main

import "fmt"

type getUserName func(string, string) string

type User struct {
    firstName    string
    lastName     string
    email        string
    getUserName  getUserName
}

func main() {

    u := User{
        firstName:      "John",
        lastName: 		"Cena",
        email:        	"[email protected]",
        getUserName: func(firstName string, lastName string) string {
            return fmt.Sprintf(firstName + " " + lastName)
        },
    }

    fmt.Printf(u.getUserName(u.firstName, u.lastName))
}

Output:

John Cena

Go - struct constructor example

There are no built-in constructors in Go. Programmers sometimes create constructor functions as a best practice.

In the example, we have the newUser constructor function, which creates new User structs. The function returns a pointer to the newly created struct.

package main

import "fmt"

type User struct {
	firstName string
	lastName  string
	email     string
}

func newUser(firstName string, lastName string, email string) *User {

	user := User{firstName, lastName, email}
	return &user
}

func main() {

	user := newUser("John", "Cena", "[email protected]")

	fmt.Println("firstName:", user.firstName)
	fmt.Println("lastName:", user.lastName)
	fmt.Println("email:", user.email)
}

Output:

firstName: John
lastName: Cena
email: [email protected]

Golang Related Tutorials

Comments