Golang Class - Tutorial Kart

[Pages:3]Golang Class

Class in Golang

Golang does not have a keyword class . But it does not limit you to define a class in Golang. You can define a class which is defining variables and methods of a class, by providing set of methods on the common type.

For example, you can define a class by defining a struct and implementing methods on the struct type. And this struct will be analogous to a class concept.

Example ? Golang Class

In the following example, we will define a class in golang using Golang structures.

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

The Student struct is analogous to a class below provided in pseudo code.

Pseudo Code

class Student{ name rollnumber function PrintDetails(){ // print details using this.name, this.rollnumber }

}

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 - Get Index of Substr 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