Skip to main content

Working with Structs in Golang

In our previous tutorial, we have explained to work with Date and Time in Golang. In this tutorial, we will explain how to work with Structs in Golang.

Structs in Golang is a ability to create user-defined data type from group of data fields with different data types. The data field in a struct can be declared with built-in or user-defined data types.

The concept of struct can be compared with the object-oriented programming which supports composition but not inheritance.

Also, read:

Declare Struct

We can represent the properties or fields from any real-world entity into a struct. For example, an employee has properties such as firstName, lastName, age, phone, address and salary. We can group these properties into a Employee struct like below.

type Employee struct {  
    firstName string
    lastName  string
	age       int
    phone     int
	address   string
	salary    int
}

The above will declare a names struct type Employee with fields as it creates a new data type Employee. We can also declare the data fields with same type in a single line as comma separated data fields like below.

type Employee struct {  
    firstName, lastName, address string
    age, phone, salary int    
}

Create Instance of Struct

We can create instance of struct by assinging to a variable.

var emp = Employee 

or

emp := Employee 

We can also create instance by using new keyword.

emp := new(Employee) 

Here is the complete example code to create instance of Employee struct and pass data field values. We will also access data field values with . operator.

package main
 
import "fmt"
 
type Employee struct {  
    firstName, lastName, address string
    age, phone, salary int    
}
 
func main() {	
	emp := Employee{firstName: "Jhon", lastName: "Smith", age: 35, phone: 123456789, salary: 50000, address: "Newyork"} 	
	fmt.Println("Employee name : ", emp.firstName, emp.lastName)
	fmt.Println("Employee age : ", emp.age)
	fmt.Println("Employee salary : ", emp.salary)
	fmt.Println("Employee phone : ", emp.phone)
	fmt.Println("Employee address : ", emp.address)	
}

The above example code will output following:

Employee name :  Jhon Smith
Employee age :  35
Employee salary :  50000
Employee phone :  123456789
Employee address :  Newyork

Struct Instance using Pointer

We can instance of struct using pointer address operator & symbol.

package main
 
import "fmt"
 
type Employee struct {  
    firstName, lastName, address string
    age, phone, salary int    
}
 
func main() {	
	emp1 := &Employee{"Kane", "William", "London", 30, 123456789, 40000} 	
	fmt.Println(emp1)
	
	emp2 := &Employee{firstName: "Jhon", lastName: "Smith", age: 35, phone: 123456789, salary: 50000, address: "Newyork"} 	
	fmt.Println(emp2)
}

The above example code will output following:

&{Kane William London 30 123456789 40000}
&{Jhon Smith Newyork 35 123456789 50000}

Nested structs

We can create nested struct that contain struct in it. In below example, we will use Address struct inside Employee struct for employee city, state and country to create address.

package main
 
import "fmt"
 
type Employee struct {  
    firstName, lastName string
    age, phone, salary int    
	address Address
}

type Address struct {  
    city  string
    state string
	country string
}
 
func main() {	
	emp := Employee{
		firstName: "Jhon", 
		lastName: "Smith", 
		age: 35, 
		phone: 123456789, 
		salary: 50000, 
		address: Address{
			city:  "Chicago",
			state: "Illinois",
			country: "USA",
		},
	} 
	
	fmt.Println("Employee name : ", emp.firstName, emp.lastName)
	fmt.Println("Employee city : ", emp.address.city)	
	fmt.Println("Employee city : ", emp.address.state)
	fmt.Println("Employee city : ", emp.address.country)
}

The above example code will output following:

Employee name :  Jhon Smith
Employee city :  Chicago
Employee city :  Illinois
Employee city :  USA

You may also like: