Gopher

Structures in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

Structures help you to combine elements of various different types into one single type. If you are familiar with object oriented programming languages then structures are equivalent to classes. Structures in Golang are somewhat similar to structures from other programming languages such as C and C++.

A Structure can have different elements of different types or similar elements of similar data type.

Syntax for declaring a structure type

Given below is the common syntax used for defining a structure data type in Golang.

type structureName struct{
  fieldName1 fieldType1
  fieldName2 fieldType2
  fieldName3 fieldType3
}

If name of structure begins with a capital letter, then it is considered to be an exported data type that can also be accessed by other packages.

If name of structure does not begin with a capital letter, then it is not an exported data type and it can not be read by other packages.

If field name of a structure data type begins with a capital letter then that field can be read by other packages as well.

If field name of a structure data type begins with a non-capital letter than that field can not be read by other packages and it is limited to the current package.

Declaration initialization of structures

Program given below declares and initializes a basic structure data type. As you can see in the following program, a structure is nothing but a blue-print or a schema of various individual fields.

package main

import "fmt"

// declaring blueprint of person structure
type person struct {
	firstName string
	lastName  string
	age       int
}

func main() {

	// creating an instance of person structure
	var p = person{
		firstName: "abc",
		lastName:  "xyz",
		age:       18,
	}
	
	// printing values present inside person structure
	fmt.Println(p)
}

Program output

Above program produces following output:

{abc xyz 18}

Zero value structure or nil structure

Program given below makes use of a zero value data structure. When a zero value data structure is used, its individual fields are given their own zero values

In the following program, zero value data structure is used and therefore modelName and noOfWheels are set to their zero values.

For example, refer to the program given below:

package main

import "fmt"

// a basic car data structure
type car struct{
	modelName string
	noOfWheels int
}

func main() {
	var c car
	// print zero value to console
	fmt.Println(c)
}

Program output

Above program produces following output:

{ 0}

Accessing and Mutating a struct fields

Program given below demonstrates how to access and mutate individual struct fields.

To access or mutate individual struct fields, we must make use of the . operator as displayed in the example given below.

package main

import "fmt"

type player struct{
	id int
	name string
	level int
	gold int
}

func main() {
	// defining an empty player structure
	var p player

	// mutating player fields
	p.id=1
	p.name="Some Player"
	p.level=5
	p.gold=1000

	// accessing individual player fields
	fmt.Println("p.id:",p.id)
	fmt.Println("p.name:",p.name)
	fmt.Println("p.level:",p.level)
	fmt.Println("p.gold:",p.gold)
}

Program output

Above program produces following output:

p.id: 1
p.name: Some Player
p.level: 5
p.gold: 1000

Using struct literals for initializing a structure

We can use struct literals as a short hand syntax for declaring and initializing a structure.

Comma , after field value is mandatory while using a struct literal.

Use of struct literals for initializing a structure is demonstrated in the example given below:

package main

import "fmt"

// blue-print
type employee struct {
	id     int
	name   string
	salary int
}

func main() {
	// struct literal
	emp := employee{
		id: 1, // the comma after field value is mandatory
		name: "some name",
		salary: 50000,
	}

	// print value of structure to the console
	fmt.Println(emp)
}

Program output

Above program produces following output:

{1 some name 50000}

Initializing structure without field names

A structure can be initialized without actually providing field names as demonstrated in the example given below. However, while doing so it is mandatory to provide values for all fields in order for initialization to be successful.

// struct without field names
package main

import "fmt"

// blue-print
type company struct {
	id     int
	name   string
	salary int
}

func main() {
	// struct literal without field names
	emp := company{
		1, // the comma after field value is mandatory
		"company x",
		20000,
	}

	// print value of structure to the console
	fmt.Println(emp)
}

Program output

Above program produces following output:

{1 some name 50000}