Created
June 30, 2022 19:28
-
-
Save 11fl/1f4cc1250e9bd1d5b6e0f9f79952f1c8 to your computer and use it in GitHub Desktop.
Pop given element from slice and return it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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