Golang Index

Methods

Methods in Go.

Example Methods

package main

// Item is a thing
type Item struct {
    Name string
    Qty  int
    cfg  string
}

// GetCfg gets the configuration string of the Item
func (i *Item) GetCfg() string {
    return i.cfg
}

// SetCfg sets a configuration string on the Item
func (i *Item) SetCfg(cfg string) *Item {
    i.cfg = cfg
    return i
}

func main() {
}

Constructors

Allocation with new allows you to construct a Type.

item := new(Item)

You can create constructor functions as follows:

func NewItem() *Item {
	item := new(Item)
	// Perform operations on item here.
    return item
}

Construct a new item as follows:

item := NewItem()

Example Code

Resources