types

type Target struct {
	Host string
	Port int
}

func (t Target) String() string {
	return fmt.Sprintf("%s:%d", t.Host, t.Port)
}

That method ran

Structs

A struct groups related fields Exported fields start with a capital letter Unexported fields stay inside the package

Slices

ports := []int{22, 80, 443}
ports = append(ports, 8080)
slices.Contains(ports, 443)

append may allocate a new backing array Never keep a stale slice header after appending through another reference

Maps

ports := map[string]int{"http": 80, "https": 443}

Reading a missing key gives the zero value Use the two value form when you need to know whether the key existed

strings

strings.ToUpper , strings.Contains , strings.Fields Check the standard library before writing a helper Most simple string work is already there

Next

Control flow in 03 control

Posted