Go Struct

In Go lang, struct (structure) is used to store multiple values of different data types in a single variable. Go does not support class unlike other object-oriented languages like C# and Java. So, the struct is used like the class in go.

Struct in Go is declared using the type and struct keywords.

Syntax:
type <struct_name> struct {
    <field_name1> datatype
    <field_name2> datatype
    <field_name3> datatype
    ...
    <field_nameN> datatype
}

In the following example, a struct named Student is created with id, name, and grade as its fields.

Example: Declaring a Struct
type Student struct {
	id int
	name string
	grade int
}

Add Function to Struct Type

A struct can include functions. In Go, any function followed after type becomes part of the type. So, you can define a function after type declaration which becomes the struct's function.

Example: Struct Instances
package main

import "fmt"

type Student struct {
	id int
	name string
	grade int
}

func (s Student) Display(){
	fmt.Println(s)
}

func main() {
    var s2 = Student{2, "Steffi", 10} 
    s2.Display()

    var s3 = Student{id:3, name:"Steve" } 
    s3.Display() //output: {3 Steve 0}
}

Output:
{2 Steffi 10}
{3 Steve 0}

Create Instances

You can assign values to the fields of a struct using curly bracket and assign it to a variable. This is called instance. You can create multiple instances of the struct with different values.

Example: Struct Instances
var s1 = Student{1, "Yash", 6} //struct instance
fmt.Println(s1) //output: {1 Yash 6}

var s2 = Student{2, "Steffi", 10} // struct instance
fmt.Println(s2) //output: {2 Steffi 10}

Note that you must assign values to all the fields in the same order as in the struct declaration.

Use named parameters like name: to assign values in a different order or to some fields, as shown below. The field whose value is not assigned will have the default value of its datatype.

Example: Struct Instances
var s1 = Student{id:1, name:"Yash", grade:6} 
fmt.Println(s1) //output: {1 Yash 6}

var s2 = Student{grade:10, id:2, name:"Steffi"} 
fmt.Println(s2) //output: {2 Steffi 10}

var s3 = Student{id:3, name:"Steve" } 
fmt.Println(s3) //output: {3 Steve 0}

You can declare an instance and assign values later on using a dot, as shown below.

Example: Struct Instances
var s4 Student
s4.id = 4
s4.name = "Abdul"
s4.grade = 8

fmt.Println(s4) //output: {4 Abdul 8}

var s5 Student
s5.name = "Sachin"

fmt.Println(s5) //output: {0 Sachin 0}

Struct Instantiation using Shorthand Syntax

Use shorthand variable syntax to create an instance of the struct, as shown below.

Example: Struct Instances
s1 := Student{1, "Yash", 6} //struct literal
fmt.Println(s1) //output: {1 Yash 6}

s2 := Student{id:2, grade:8 } //assign values in different order
fmt.Println(s2) //output: {2 8}

Struct Instantiation using new Keyword

A struct can be declared using the new keyword and assign values using dot notation.

Example: Struct Instances
var s4 = new(Student)
s4.id = 4
s4.name = "Abdul"
s4.grade = 8

fmt.Println(s4) //output: &{4 Abdul 8}

Struct Instantiation using Pointer

An intance of a struct can be created using pointer symbol & and pass values in the curly brackets, as shown below.

Example: Struct Instance using Pointer
var s4 = &Student{ 4, "Abdul", 8}

fmt.Println(s4) //output: &{4 Abdul 8}

Access Struct

Use the dot notation to access any field in a struct.

Example: Access Structs
type Student struct {
		id int
		name string
		grade int
	}

func main() {
	s1 := Student{1, "Yash", 6}
	
	//Accessing s1 values
	fmt.Println("Name: ", s1.name) 
	fmt.Println("ID: ", s1.id)  
	fmt.Println("Grade: ", s1.grade)  
}
Output:
Name:  Yash
ID:  1
Grade:  6

Update Struct

You can update the values of an instance of the struct using dot notation, as shown below.

Example: Update Structs
type Student struct {
		id int
		name string
		grade int
	}

func main() {
	s1 := Student{1, "Steve", 6}
	
	s1.id = 10
    s1.name = "Heer"
    s1.grade = 8

	fmt.Println(s1.name, s1.id, s1.grade)  //output: 10 Heer 8
}

Passing struct as a Function Argument

A struct can be passed to a function as an argument in the same way as any other variable.

Example: PAssing Struct to Function
package main
import "fmt"

type Student struct {
		id int
		name string
		grade int
	}

func main() {
	var s1 Student
	s1.id = 10
	s1.name = "Steve"
	s1.grade = 3

	//pass s1 to printStudentDetails()
	printStudentDetails(s1)
}

func printStudentDetails(student Student) {
	fmt.Println("Name: ",  student.name) 
	fmt.Println("ID: ",  student.id)  
	fmt.Println("Grade: ",  student.grade)  
}
Output:
Name:  Steve
ID:  10
Grade:  3

In the above program, the printStudentDetails() function accepts an input parameter of type Student. The variable s1 of type Student is declared in the main function and is passed as an argument to the printStudentDetails() function.