Codecademy Logo

Learn Go: Structs

Structs and Fields

In Go, a group of related variables can be defined as a struct. Each variable within a struct is known as a field.

Struct Definition

In Go, a struct must be defined before it can be used in a program. The definition of a struct includes its name and its fields.

type Point struct{
x int
y int
}

Struct Instances

In Go, an instance of a defined struct can be created by providing its name followed by a set of curly braces with optional values.

p1 := Point{x: 10, y: 12}

Struct Methods

In Go, methods can be associated with a struct by naming a struct parameter in parentheses before the function name.

func (rectangle Rectangle) area() float32{
return rectangle.length * rectangle.height
}
func main() {
rect.area()
}

Access Struct Fields

In Go, fields within a struct can be accessed or modified using the . operator.

p1 := Point{x:10, y:12}
fmt.Println(p1.x)

Passing Structs as Pointers

In Go, the values of a struct can only be modified in a function if the struct is passed as a pointer.

func (rectangle *Rectangle) modify(newLength float32){
rectangle.length = newLength
}

Access Pointer Struct Fields

In Go, accessing the fields of a pointer to a struct does not require dereferencing. The fields of the struct pointer can be accessed using the normal . syntax.

steve := Employee{“Steve”, “Stevens”, 34, “Junior Manager”}
pointerToSteve := &steve
fmt.Println(pointerToSteve.firstName)

Arrays of Structs

In Go, arrays can be used to store many of the same struct’s instances.

points := []Point{{1, 1}, {7, 27}, {12, 7}, {9, 25}}

Nested Structs

In Go, a struct can contain fields that are themselves other structs.

type Name struct{
firstName string
lastName string
}
type Employee struct{
name Name
age int
title string
}

Learn More on Codecademy