Golang - struct

Golang ? struct

Golang Structure

Golang Structure is a datatype that allows you to store different properties under a single variable name. It is similar to a class, in object oriented programming, that has only properties. It is very similar to structure in C programming.

In this tutorial, we will learn how to define a structure, declare variables of this structure type and initialize structure variables.

Golang Structure Declaration

To declare a Golang Structure, use the keywords type and struct. The syntax to declare a go structure is:

type struct_name struct { member definition; member definition; ... member definition;

}

Following is an example, to declare student structure datatype with properties: name and roll number.

package main type student struct {

name string rollNumber int } func main() { }

Declare Variables of the Structure Datatype

Now we will declare variables of the structure datatype that is declared above.

example.go

package main type student struct {

name string rollNumber int } func main() { // declare variables of Student type var student1 student var student2 student }

Initialize Structure Datatype Variables

We can either initialize the structure variable during its declaration or in another statement.

example.go

package main type student struct {

name string rollNumber int } func main() { // intialize during declaration var student1 = student{name:"Anu", rollNumber:14} var student2 student // intialize explicitly student2 = student{name:"Arjit", rollNumber:13} }

Dot Operator to Access Structure Members

To access members of a structure, use dot . operator. You can access a specific member by writing structure variable, followed by dot and then the member variable name.

In the following example, we have used dot operator to set the value of member of the structure variable and also to get the value of a member.

example.go

package main import "fmt" type student struct {

name string

name string rollNumber int }

func main() { var student1 = student{name:"Anu", rollNumber:14} // get the member value fmt.Printf("student1 details\n--------------\n") fmt.Printf("Name : %s\n", student1.name) fmt.Printf("Rollnumber : %d\n", student1.rollNumber)

var student2 = student{name:"Arjit", rollNumber:13} // set the member value student2.rollNumber=24 student2.name = "Bungi"

fmt.Printf("\nstudent2 details\n--------------\n") fmt.Printf("Name : %s\n", student2.name) fmt.Printf("Rollnumber : %d\n", student2.rollNumber) }

Output

student1 details -------------Name : Anu Rollnumber : 14

student2 details -------------Name : Bungi Rollnumber : 24

Golang Methods on Struct

You can associate behavior as well to structs, in addition to properties, in the form of functions. In the following example, we have a struct defined, and an associated method to the Student struct.

example.go

package main

import "fmt"

// struct definition type Student struct {

name string rollNumber int }

// associate method to Strudent struct type func (s Student) PrintDetails() {

fmt.Println("Student Details\n---------------") fmt.Println("Name :", s.name) fmt.Println("Roll Number :", s.rollNumber) }

} func main() {

var stud1 = Student{name:"Anu", rollNumber:14} stud1.PrintDetails() }

Output

Student Details --------------Name : Anu Roll Number : 14

Golang Golang Tutorial Run Golang Program Golang If Else Golang Switch Golang For Loop Golang Comments Golang Functions Golang Array Golang Slice Golang Struct Golang Class Golang Range Golang Map Golang Goroutine Golang Channel

Golang String Operations Golang String Length Golang String Concatenation Golang Split String Golang String - Get Index of Substr Golang String to Uppercase

Golang String to Uppercase Golang String to Lowercase Golang Convert String to Integer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download