Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Struct

Go have the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. The declaration of new data type is constructed to provide the compiler with size and representation information, similar like built-in data types.

Structs are the only way to create concrete user-defined types in Go. Struct types are declared by composing a fixed set of unique fields. Each field in a struct is declared with a known type, which could be a built-in type or another userdefined type.

Declaration of a struct type

The declaration starts with the keyword type, then a name for the new type, and finally the keyword struct.

type Rectangle struct {
	Length  float64
	Breadth float64
	Height  float64
}

The type keyword introduces a new type. It's followed by the name of the type (Rectangle), the keyword struct to indicate that we are defining a struct type, and a list of fields inside of curly braces. The identifiers name here started with an uppercase letter means accessibility not limited within the package and they are exported to other packages also.

If a group of struct fields have a common type, then we can also organize the fields of the same type in a single-line statement.

type Rectangle struct {
	Length, Breadth, Height float64	 
}

Go provides support for composition of types which enables us to create larger types by combining smaller types. Go does not provide support for inheritance.

Declaration of a variable of the struct type set to its zero value

We can create an instance of our new Rectangle type in a variety of ways:

var r Rectangle

When we declare variables, the value that the variable represents is always initialized. The value can be initialized with a specific value or it can be initialized to its zero value, which is the default value for that variable's type.
Like with other data types, this will create a local Rectangle variable that is by default set to zero. For a struct, zero means each of the fields is set to their corresponding zero value (0 for ints, 0.0 for floats, "" for strings, nil for pointers, etc.)

Declaration of a variable of the struct type using a struct literal

// Declare a variable of type Employee and initialize all the fields.
e := Employee{
	FirstName: "Mark",
	LastName: "Jones",
	Email: "mark@gmail.com",
	Age: 25,
	Permanent: false,
}

When we are creating and initializing a struct type, we have to use a struct literal to perform the initialization. The struct literal takes the form of curly brackets with the initialization declared within them.

Creating a Struct type Using a Struct Literal by Specifying Values to a Few Fields

Employee{
	FirstName: "Mark",
	LastName: "Jones",
	Email: "mark@gmail.com",
	Age:25,
	Permanent:false,
}

Creating a struct type value without declaring the field names

The order of the values does matter in this case and needs to match the order of the fields in the struct declaration.

Employee{
	"Mark", "Jones", "mark@gmail.com", 25, false
}

Creating a Struct type using other struct types as the type for the fields of structs.

Employee Struct with a slice of a user-defined Type as the Type for field, the Employee struct expanded by adding a new field with its type as a slice of Salary type.

type Salary struct {
	Basic, HRA, TA  float64	
}
type Employee struct {
	FirstName, LastName, Email string
	Age int
	MonthlySalary []Salary
}

The Employee struct has been expanded by adding a new field MonthlySalary, for which the type is specified as a slice of a struct named Salary. Using the MonthlySalary field, you can specify monthly salary for an employee.

// GO language program with an example of Struc Type
package main
import (
"fmt"
)
func main() {
	type Salary struct{
		Basic, HRA, TA float64		
	}
	type Employee struct{
		FirstName, LastName, Email string
		Age int
		MonthlySalary []Salary
	}
	e := Employee{
		FirstName: "Mark",
		LastName: "Jones",
		Email: "mark@gmail.com",
		Age: 25,
		MonthlySalary: []Salary{
			Salary{
				Basic:15000.00,
				HRA:5000.00,
				TA:2000.00,
			},
			Salary{
				Basic:16000.00,
				HRA:5000.00,
				TA:2100.00,
			},
			Salary{
				Basic:17000.00,
				HRA:5000.00,
				TA:2200.00,
			},
		},
	}
	fmt.Println(e.FirstName,e.LastName)
	fmt.Println(e.Age)
	fmt.Println(e.Email)
	fmt.Println(e.MonthlySalary[0])
	fmt.Println(e.MonthlySalary[1])
	fmt.Println(e.MonthlySalary[2])
}

Adding Methods to Struct Types

Go language type system allows you to add methods to struct types using a method receiver. The method receiver specifies which type has to associate a function as a method to that type.

// GO language program with an example of Struc Type with Method
package main
import (
"fmt"
)
type Salary struct{
		Basic, HRA, TA float64		
	}
	type Employee struct{
		FirstName, LastName, Email string
		Age int
		MonthlySalary []Salary
	}
func (e Employee) EmpInfo() string{
	fmt.Println(e.FirstName,e.LastName)
	fmt.Println(e.Age)
	fmt.Println(e.Email)
	for _, info := range e.MonthlySalary {
		fmt.Println("===================")
		fmt.Println(info.Basic)
		fmt.Println(info.HRA)
		fmt.Println(info.TA)		
	}
	return "----------------------"
}
func main() {	
	e := Employee{
		FirstName: "Mark",
		LastName: "Jones",
		Email: "mark@gmail.com",
		Age: 25,
		MonthlySalary: []Salary{
			Salary{
				Basic:15000.00,
				HRA:5000.00,
				TA:2000.00,
			},
			Salary{
				Basic:16000.00,
				HRA:5000.00,
				TA:2100.00,
			},
			Salary{
				Basic:17000.00,
				HRA:5000.00,
				TA:2200.00,
			},
		},
	}
	fmt.Println(e.EmpInfo())	
}

GO language program with an example of Struc Type with Method