Advertisement
cwchen

[Go] Removing an element from a slice.

Sep 20th, 2017
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.39 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5. )
  6.  
  7. func main() {
  8.     slice := []int{1, 2, 3, 4, 5}
  9.  
  10.     if !(len(slice) == 5) {
  11.         log.Fatal("Wrong length")
  12.     }
  13.  
  14.     if !(slice[2] == 3) {
  15.         log.Fatal("Wrong value")
  16.     }
  17.  
  18.     // Remove the 3rd element.
  19.     slice = append(slice[0:2], slice[3:5]...)
  20.  
  21.     if !(len(slice) == 4) {
  22.         log.Fatal("Wrong length")
  23.     }
  24.  
  25.     if !(slice[2] == 4) {
  26.         log.Fatal("Wrong value")
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement