go struct with function code example
Example 1: go create new instance of struct
type Person struct {
Name string
}
func main() {
var me Person
me.name = "mattalui"
}
Example 2: go structs
type Vertex struct {
X, Y int
}
var v = Vertex{1, 2}
var v = Vertex{X: 1, Y: 2}
var v = []Vertex{{1,2},{5,2},{5,5}}
v.X = 4
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
v.Abs()
func (v *Vertex) add(n float64) {
v.X += n
v.Y += n
}
Example 3: go struct methods
type Person struct {
name string
surname string
}
func (p Person) GetName() string {
return p.name
}
func (p Person) GetSurname() string {
return p.surname
}