Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSS

Methods in Golang

package main
import "fmt"

// struct defination
type person struct {
  name   string
  age    int
  gender string
}

// method defination
func (p *person) describe() {
  fmt.Printf("%v is %v years old.", p.name, p.age)
}
func (p *person) setAge(age int) {
  p.age = age
}

func (p person) setName(name string) {
  p.name = name
}

func main() {
  pp := &person{name: "Bob", age: 42, gender: "Male"}
  pp.describe()
  // => Bob is 42 years old
  pp.setAge(45)
  fmt.Println(pp.age)
  //=> 45
  pp.setName("Hari")
  fmt.Println(pp.name)
  //=> Bob
}
Source by gowithgolang.com #
 
PREVIOUS NEXT
Tagged: #Methods #Golang
ADD COMMENT
Topic
Name
9+6 =