Pop from slice

This is an example Go program demonstrating a pop-like operation using a slice of strings.

package main

import (
        "fmt"
)

func main() {
        a := []string{
                "mentos",
                "hi-chew",
                "puccho",
        }
        var x string
        for len(a) > 0 {
                x, a = a[len(a)-1], a[:len(a)-1]
                fmt.Println(x)
        }
}

(download)

The output of the example looks like this:

puccho
hi-chew
mentos

Web links


Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer