Go Programming Language structs

Go Programming Language structs

In Go programming, structs are used to create a typed collections of fields and useful to group the data together to form records.

To define a struct, we can use struct keyword.

Syntax:
type struct_name struct {
// fields
}

Go Struct Example

Explains how to create struct in Go programming, creating instance, setting values to struct instance, reading and printing struct instances.

package main                                                               	 
                                                                           	 
import (                                                                   	 
	"fmt"                                                                  	 
	"time"                                                                 	 
)                                                                          	 
                                                                           	 
//define a struct 'Student'                                                	 
type Student struct {                                                      	 
	id int                                                                 	 
	name string                                                            	 
	grade int                                                              	 
	created time.Time                                                      	 
}                                                                          	 
                                                                           	 
func main() {                                                              	 
	var student1 Student                                                   	 
	student2 := new(Student)                                               	 
                                                                           	 
	// setting values                                                      	 
	student1.id = 1                                                        	 
	student1.name = "Student1"                                             	 
	student1.grade = 10                                                    	 
	student1.created = time.Now()                                          	 
                                                                           	 
	student2.id = 2                                                        	 
	student2.name = "Student2"                                             	 
	student2.grade = 8                                                     	 
	student2.created = time.Now()                                          	 
                                                                           	 
	// displays student details                                            	 
	fmt.Println("Student Details")                                         	 
	fmt.Println("----------------")                                        	 
	fmt.Printf("Id: %d\n", student1.id)                                    	 
	fmt.Printf("Name: %s\n", student1.name)                                	 
	fmt.Printf("Grade: %d\n", student1.grade)                              	 
	fmt.Printf("Created: %s\n", student1.created.String())                 	 
                                                                           	 
	fmt.Println("----------------")                                        	 
	fmt.Printf("Id: %d\n", student2.id)                                    	 
	fmt.Printf("Name: %s\n", student2.name)                                	 
	fmt.Printf("Grade: %d\n", student2.grade)                              	 
	fmt.Printf("Created: %s\n", student2.created.String())                 	 
}                                                                          	 
 
Output:
$ go build structs.go
$ ./structs
Student Details
----------------
Id: 1
Name: Student1
Grade: 10
Created: 2018-07-25 22:05:58.588872315 +0530 IST
----------------
Id: 2
Name: Student2
Grade: 8
Created: 2018-07-25 22:05:58.588872499 +0530 IST