slice
package main

import (
	"fmt"
)

func main() {
	var s1 []int
	var s2 = []int{}
	if __ == nil {
		fmt.Println("nil slice")
	}
	if __ != nil {
		fmt.Println("empty slice")
	}
}
  • A. s1, s2
  • B. s2, s1
  • C. s1, s1
  • D. s2, s2
Answer
Try it
A
slice1
package main

import (
	"fmt"
)

func main() {
	s := [3]int{1, 2, 3}
	a := s[:0]
	b := s[:2]
	c := s[1:2:cap(s)]
	fmt.Println(len(a), cap(a))
	fmt.Println(len(b), cap(b))
	fmt.Println(len(c), cap(c))
}
Answer
Try it
0 3
2 3
1 2
slice2
package main

import "fmt"

func main() {
	s1 := []int{1, 2, 3}
	s2 := s1[1:]
	s2[1] = 4
	fmt.Println(s1)
	s2 = append(s2, 5, 6, 7)
	fmt.Println(s1)
}
Answer
Try it
[1 2 4]
[1 2 4]