Last item in a slice

yourbasic.org/golang

To read the last element:

a := []string{"A", "B", "C"}
s := a[len(a)-1] // C

To remove it:

a = a[:len(a)-1] // [A B]

(Go doesn’t have negative indexing like Python does.)

Watch out for memory leaks

If the slice is permanent and the element temporary, you may want to remove the reference before removing the element from the slice.

a[len(a)-1] = "" // Erase element (write zero value)
a = a[:len(a)-1] // [A B]

Further reading

Slices in a nutshell