Advertisement
cwchen

[Go] Creating a slice from an array.

Sep 20th, 2017
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.43 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "reflect"
  7. )
  8.  
  9. func main() {
  10.     langs := [4]string{"Go", "Python", "Ruby", "PHP"}
  11.     slice := langs[0:4] // Upper bound excluded.
  12.  
  13.     // Print out the types of these variables
  14.     fmt.Println(reflect.TypeOf(langs))
  15.     fmt.Println(reflect.TypeOf(slice))
  16.  
  17.     if !(langs[3] == "PHP") {
  18.         log.Fatal("Wrong value")
  19.     }
  20.  
  21.     slice[3] = "Perl"
  22.  
  23.     if !(langs[3] == "Perl") {
  24.         log.Fatal("Wrong value")
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement