Structures in Go

Grouping together typed data to form records.

syntax

type dragon struct {
    Name string
    Height int
}

Basic struct call example

package main

import (
	"fmt"
)

type dragon struct {
	Name string
	Height int
	
}

func main() {
dragons := dragon{"trex", 10000}
fmt.Println(dragons.Height)

// you can also assign using dot operator
dragon.name = "participants"

}

This shorter declaration form has a few drawbacks that have led the Go community to prefer the longer form in most circumstances. You must provide values for each field in the struct when using the short declaration—you can’t skip fields you don’t care about. This quickly causes short declarations for structs with many fields to become confusing. For this reason, declaring structs using the short form is typically used with structs that have few fields.

package main

import "fmt"

type employee struct {
  Name string
  ID int
  Address address
}

type address struct{
  Street string
  City string
  State string
  Country string
  ZipCode int
  region string
}


func main() {

  details := employee{"akshay", 1234, address{"123", "x-mesto", "Prague", "Czech Republic", 60210, "EU"}}
  fmt.Println(details)
  
  
}

The field names in the examples so far have all begun with capital letters. This is more significant than a stylistic preference. The use of capital or lowercase letters for field names affects whether your field names will be accessible to code running in other packages.

Nested struct

package main

import "fmt"

type employee struct {
  Name string
  ID int
  Address address
}

type address struct{
  Street string
  City string
  State string
  Country string
  ZipCode int
  region string
}


func main() {

  details := employee{"shariq", 1234, address{"415/6", "Brno-mesto", "Brno", "Czech Republic", 60200, "EU"}}
  fmt.Println(details)
  
  // you must call method from fields and not struct name
  fmt.Println(details.Address.Street)
  
}}

Nested struct

We can also use a common struct for other structs too ! Try that.

Note: Always mention name of struct when initializing struct inside struct

struct with Pointer

package main

import (
	"fmt"
)

type dragon struct {
Name string
Height int

}

// without pointers
func main() {
	dragons := dragon{"Goku", 9000}
	Super(dragons)
	fmt.Println(dragons.Height)
}
func Super(d dragon) {
	d.Height += 10000
}

// result = 9000


// with pointers
func main() {
	dragons := &dragon{"Goku", 9000}
	Super(dragons)
	fmt.Println(dragons.Height)
}
func Super(d *dragon) {
	d.Height += 10000
}

// result = 19000

& is used to get address, * is to extract the value from the address

Return from function

package main

import (
	"fmt"
)

type dragon struct {
Name string
Height int

}

func main() {

dragons := &dragon{"trex", 9000}
a, _ := Super(dragons)
fmt.Println(dragons.Power)
// try fmt.Println(a,_)
}

func Super(s *dragon) (string, int) {
s = &dragon{"Hohan", 4242}
return s.Name, s.Height
}

methods on structure

package main

import (
	"fmt"
)

type dragon struct {
	Name string
	Height int
}

func main() {

	dragons := &dragon{"trex", 9000}
	fmt.Println(dragons)
	dragons.super()
	fmt.Println(dragons.Height)
	// try fmt.Println(a,_)
}

func (s *dragon) super() {
	s.Height += 10000
}

Last updated

Was this helpful?