Go Structures

Go Language - Structures

Structures is used to combine data items of different kinds. Generally, it is to represent a record which will containdifferent data types. To define a structure, we need two keyword type andstruct. The Structkeyword defines new data type, with multiple datamembers foranyprogram. The type keyword will bind name with struct.

Syntax
type struct_variable_name struct{
	member definition;
	member definition;
	......
	member definition;
	}

To declare variable for the type. The Syntax is :

Syntax
Variable_name := struct_variable_name {
		value1, value2,.., valuen
	}

To accessing structures member, we can use the member access operator (dot). The member access operator will be placed between the struct variable name and structure member to use the value of the member in structure.

Program Example :

CODE/PROGRAM/EXAMPLE
package main 
import “fmt”
type student struct{
	name string
	register_no int
	mark1 float
	mark2 float
	mark3float
	}
	func main(){
		var Student1 student
		Student1.name=” John;
		Student1.register_no=13456
		Student1.mark1=82
		Student1.mark2=54
		Student1.mark3=76
		fmt.Printf(“Name of the Student: %s\n”, Student1.name)
		fmt.Printf(“Register no of the Student: %s\n”, Student1.register_no)
}

Output :
Name of the Student: John
Register noof the Student: 13456

It similar to use the structure as function arguments as other variable.

CODE/PROGRAM/EXAMPLE
package main
import “fmt”
type student struct{
	name string
	register_no int
	mark1 float
	mark2 float
	mark3 float
	}
	func main(){
		var Student1 student
		Student1.name=” John;
		Student1.register_no=13456
		Student1.mark1=82
		Student1.mark2=54
		Student1.mark3=76
		fmt.Printf(“Name of the Student: %s\n”, Student1.name)
		fmt.Printf(“Register no of the Student: %s\n”, Student1.register_no)
		total(Student1)
		}
		func total(Student s){
			var total float
			total= s.mark1 + s.mark2 + s.mark3
			fmt.Printf(“Total marks of the Student: %f\n”,total)
			}
			
Output : Name of the Student: John
Register no of the Student: 13456
Total marks of the Student:212

To define pointers to structures, the syntax is :

Syntax
var struct_pointer *Student

Also, we can store the address of the structure variable by placing & operator before the name of the variable

Syntax
struct_pointer =&Student1;

To access the member of a structure using a pointer we can use the ‘.’ operator.

Syntax
struct_pointer.name;

Program Example :

CODE/PROGRAM/EXAMPLE
package main
import “fmt”
type student struct{
	name string
	register_no int
	mark1 float
	mark2 float
	mark3float
	}
	func main(){
		var Student1 student
		Student1.name="John";
		Student1.register_no=13456
		Student1.mark1=82
		Student1.mark2=54
		Student1.mark3=76
		fmt.Printf(“Name of the Student: %s\n”, Student1.name)
		fmt.Printf(“Register no of the Student: %s\n”, Student1.register_no)
		total(&Student1)
		}
		func total(Student *s){
			var total float
			total=s.mark1+ s.mark2+ s.mark3
			fmt.Printf(“Total marks of the Student: %f\n”,total)
			}
			
Output :
Name of the Student: John
Register no of the Student: 13456
Total marks of the Student:212
#Go_Language_Structures #golang_structure #golang_struct #golang_project_structure #go_struct #golang_structure_example

(New page will open, for Comment)

Not yet commented...