How do I (succinctly) remove the first element from a slice in Go?

前端 未结 2 1987
轻奢々
轻奢々 2021-02-02 06:11

I\'ve built a simple queue in Go. It uses an internal slice to keep track of its elements. Elements are pushed onto the queue by appending to the slice. I\'d like to implement <

2条回答
  •  礼貌的吻别
    2021-02-02 07:06

    Did you try these?

    Pop from queue

    x, a = a[0], a[1:]
    

    Pop from stack

    x, a = a[len(a)-1], a[:len(a)-1]
    

    Push

    a = append(a, x)
    

    From: https://code.google.com/p/go-wiki/wiki/SliceTricks

提交回复
热议问题