Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Struct methods in Go

Class methods / Struct methods can be added to struct in two appraoches.

Method on struct type
Method which takes struct as parameter


func main() {
    var student = Student{}
    student.name = "Ram"
    student.age = 10

    student.printName()
}

type Student struct { 
    name string
    age  int
}

func (student Student) printName() { // Method on struct type
    print(student.name)
}

func printName(student Student) { // Method which takes struct as parameter

    print(student.name)
}