Skip to content

Instantly share code, notes, and snippets.

@11fl
Created June 30, 2022 19:28
Show Gist options
  • Save 11fl/1f4cc1250e9bd1d5b6e0f9f79952f1c8 to your computer and use it in GitHub Desktop.
Save 11fl/1f4cc1250e9bd1d5b6e0f9f79952f1c8 to your computer and use it in GitHub Desktop.
Pop given element from slice and return it
package main
import "fmt"
func main() {
a := []string{"A", "B", "C", "D", "E", "x"}
i := 2
num, _ := pop(a, i)
fmt.Println(num)
}
//func that pops out given element from slice and returns it
func pop(s []string, i int) (string, []string) {
ret := s[i]
copy(s[i:], s[i+1:])
s = s[:len(s)-1]
return ret, s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment