文章目录
- 一. Switch语句
-
- 1. Default case
- 2. Multiple expressions in case
- 3. Expressionless switch
- 4. Fallthrough
- 5. break
- 6. break for loop
- 二. Arrays数组
-
- 1. when arrays are passed to functions as parameters
- 2. Iterating arrays using range
- 3.Multidimensional arrays 多维数组
- 三. Slices切片
-
- 1. Creating a slice
- 2. Modifying a slice
- 3. Length and capacity of a slice
- 4. Creating a slice using make
- 5. Appending to a slice
- 6. Passing a slice to a function
- 7. Multidimensional slices
- 7. Memory Optimisation
一. Switch语句
1. Default case
finger := 6
fmt.Printf("Finger %d is ", finger)
switch finger {
case 1:
fmt.Println("Thumb")
case 2:
fmt.Println("Index")
case 3:
fmt.Println("Middle")
case 4:
fmt.Println("Ring")
case 5:
fmt.Println("Pinky")
default:
fmt.Println("incorrect finger number")
}
2. Multiple expressions in case
letter := "a"
fmt.Printf("Letter %s is a ", letter)
switch letter {
case "a", "b", "c", "d":
fmt.Println("Vowel")
default:
fmt.Println("No Vowel")
}
3. Expressionless switch
num := 75
switch {
case num >= 0 && num <= 50:
fmt.Printf("%d is greater than 0 and less than 50", num)
case num >= 51 && num <= 100:
fmt.Printf("%d is greater than 51 and less than 100", num)
case num >= 101:
fmt.Printf("%d is greater than 100", num)
}
4. Fallthrough
switch num := 25; {
case num < 50:
fmt.Printf("%d is lesser than 50\n", num)
fallthrough
case num > 100:
fmt.Printf("%d is greater than 100\n", num)
}
5. break
switch num := -7; {
case num < 50:
if num < 0 {
break
}
fmt.Printf("%d is lesser than 50\n", num)
fallthrough
case num < 100:
fmt.Printf("%d is lesser than 100\n", num)
fallthrough
case num < 200:
fmt.Printf("%d is lesser than 200", num)
}
6. break for loop
randloop:
for {
switch i := rand.Intn(100); {
case i%2 == 0:
fmt.Printf("Generated even number %d", i)
break randloop
}
}
二. Arrays数组
var a [3]int
a[0] = 11
a[1] = 12
a[2] = 13
b := [3]int{15, 16, 17}
c := [3]int{18}
d := [...]int{19, 20, 21}
e := [...]string{"USA", "China", "India", "Germany", "France"}
f := e
f[0] = "Singapore"
fmt.Println("a is ", a)
fmt.Println("b is ", b)
fmt.Println(b)
fmt.Println(a)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
fmt.Println(f)
fmt.Println(len(f))
1. when arrays are passed to functions as parameters
package main
import "fmt"
func changeLocal(num [5]int) {
num[0] = 22
fmt.Println("inside function ", num)
}
func main() {
num := [...]int{33, 44, 55, 66, 77}
fmt.Println("bebore passing to function", num)
changeLocal(num)
fmt.Println("after passing to function", num)
}
2. Iterating arrays using range
a := [...]float64{13.14, 14.13, 15.20, 21, 52}
for i := 0; i < len(a); i++ {
fmt.Printf("%d th element of a is %.2f\n", i, a[i])
}
a := [...]float64{13.14, 14.13, 15.20, 21, 52}
sum := float64(0)
for i, v := range a {
fmt.Printf("%d the element of a is %.2f\n", i, v)
sum += v
}
fmt.Println("\nsum of all elements of a", sum)
3.Multidimensional arrays 多维数组
a := [...]float64{13.14, 14.13, 15.20, 21, 52}
for i := 0; i < len(a); i++ {
fmt.Printf("%d th element of a is %.2f\n", i, a[i])
}
a := [...]float64{13.14, 14.13, 15.20, 21, 52}
sum := float64(0)
for i, v := range a {
fmt.Printf("%d the element of a is %.2f\n", i, v)
sum += v
}
fmt.Println("\nsum of all elements of a", sum)
三. Slices切片
1. Creating a slice
c := []int{555, 666, 777}
fmt.Println(c)
a := [5]int{76, 77, 78, 79, 80}
var b []int = a[1:4]
fmt.Println(b)
2. Modifying a slice
darr := [...]int{57, 89, 90, 82, 100, 78, 67, 69, 59}
dslice := darr[2:5]
fmt.Println("array before", darr)
for i := range dslice {
dslice[i]++
}
fmt.Println("array after", darr)
numa := [3]int{78, 79, 80}
nums1 := numa[:]
nums2 := numa[:]
fmt.Println("array before change 1", numa)
nums1[0] = 100
fmt.Println("array after modification to slice nums1", numa)
nums2[1] = 101
fmt.Println("array after modification to slice nums2", numa)
3. Length and capacity of a slice
fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d", len(fruitslice), cap(fruitslice))
fruitslice = fruitslice[:cap(fruitslice)]
fmt.Println(fruitslice)
fmt.Println("After re-slicing length is", len(fruitslice), "and capacity is ", cap(fruitslice))
4. Creating a slice using make
package main
import (
"fmt"
)
func main() {
i := make([]int, 5, 5)
fmt.Println(i)
}
5. Appending to a slice
cars := []string{"Ferrari", "Honda", "Ford"}
fmt.Println("cars:", cars, "has old length", len(cars), "and capacity", cap(cars))
cars = append(cars, "Toyota")
fmt.Println("cars:", cars, "has new length", len(cars), "and capacity", cap(cars))
var names []string
if names == nil {
fmt.Println("Slice is nil going to append")
names = append(names, "John", "Like", "Lisa")
fmt.Println("names contents:", names)
}
6. Passing a slice to a function
func SubtactOne(numbers []int) {
for i := range numbers {
fmt.Println(i, numbers)
numbers[i] -= 2
}
}
func main() {
nos := []int{4, 5, 6}
fmt.Println("slice before function call", nos)
SubtactOne(nos)
fmt.Println("slice after function call", nos)
}
7. Multidimensional slices
pls := [][]string{
{"C", "C--"},
{"Python"},
{"JavaScript"},
{"Go", "Rust"},
}
for _, v1 := range pls {
for _, v2 := range v1 {
fmt.Printf("%s", v2)
}
fmt.Printf("\n")
}
7. Memory Optimisation
func Countries() []string {
countries := []string{"China", "USA", "Singapore", "Germany", "India", "Australia"}
neededCountries := countries[:len(countries)-2]
countriesCpy := make([]string, len(neededCountries))
copy(countriesCpy, neededCountries)
return countriesCpy
}
func main() {
countriesNeeded := Countries()
fmt.Println(countriesNeeded)
}