目录标题
- 一、Variadic Functions
-
- 1.Syntax
- 2.Examples and understanding how variadic functions work
- 3.Slice arguments vs Variadic arguments 仅改变可变参数
- 4.Gotcha
- 二、Map
-
- 1.Create a Map
- 2.Retrieving value for a key from a map
- 3.Checking if a key exists
- 4.Iterate over all elements in a map
- 5. Deleting items from a map
- 6.Map of structs
- 7.Length of the map
- 8.Maps are reference types
- 9.Maps equality
- 三、Strings
-
- 1.Accessing individual bytes of a string
- 2.Accessing individual characters of a string
- 3.String length
- 4.String comparison
- 5.String concatenation
- 6.Strings are immutable
- 四、Printf的%格式化字符串
一、Variadic Functions
1.Syntax
package main
import (
"fmt"
)
func Hello(a int, b ...int) {
fmt.Println(a, b)
}
func main() {
Hello(666, 22, 333, 555)
}
2.Examples and understanding how variadic functions work
func find(num int, nums ...int) {
fmt.Printf("type of nums is %T\n", nums)
found := false
for i, v := range nums {
if v == num {
fmt.Println(num, "found at index", i, "in", nums)
found = true
}
}
if !found {
fmt.Println(num, "not find in ", nums)
}
fmt.Printf("\n")
}
func main() {
find(89, 87, 90, 88, 89)
find(45, 56, 67, 45, 90, 109)
find(78, 38, 56, 98)
find(87)
}
nums := []int{89, 90, 95}
find(89, nums)
find(89, nums...)
3.Slice arguments vs Variadic arguments 仅改变可变参数
func find(num int, nums []int) {
fmt.Printf("type of nums is %T\n", nums)
found := false
for i, v := range nums {
if v == num {
fmt.Println(num, "found at index", i, "in", nums)
found = true
}
}
if !found {
fmt.Println(num, "not find in ", nums)
}
fmt.Printf("\n")
}
func main() {
find(89, []int{87, 90, 88, 89})
find(45, []int{56, 67, 45, 90, 109})
find(78, []int{38, 56, 98})
find(87, []int{})
}
4.Gotcha
package main
import (
"fmt"
)
func change(s ...string) {
s[0] = "Go"
s = append(s, "playground")
fmt.Println(s)
}
func main() {
welcome := []string{"hello", "world"}
change(welcome...)
fmt.Println(welcome)
}
二、Map
1.Create a Map
package main
import "fmt"
func main() {
employeeSalary := make(map[string]int)
fmt.Println(employeeSalary)
Department := map[string]string{
"Designer": "设计部",
"Research": "研发部",
}
fmt.Println(Department)
employeeSalary["Like"] = 15000
employeeSalary["Jack"] = 9000
employeeSalary["Lisa"] = 10000
fmt.Println("employeeSalary map contents:", employeeSalary)
var employeeSalary map[string]int
fmt.Println(employeeSalary)
employeeSalary["Like"] = 15000
}
2.Retrieving value for a key from a map
employeeSalary := map[string]int{
"steve": 12000,
"jamie": 15000,
"mike": 9000,
}
employee := "jamie"
salary := employeeSalary[employee]
fmt.Println("Salary of", employee, "is", salary)
fmt.Println("Salary of mike is", employeeSalary["mike"])
fmt.Println("Salary of joe is", employeeSalary["joe"])
3.Checking if a key exists
employeeSalary := map[string]int{
"steve": 12000,
"jamie": 15000,
"mike": 9000,
}
newEmp := "jamie"
value, ok := employeeSalary[newEmp]
fmt.Println(value, ok)
if ok == true {
fmt.Println(ok)
fmt.Println("Salary of", newEmp, "is", value)
return
}
fmt.Println(newEmp, "not Found")
4.Iterate over all elements in a map
employeeSalary := map[string]int{
"steve": 12000,
"jamie": 15000,
"mike": 9000,
}
fmt.Println("Contents of the map")
for key, value := range employeeSalary {
fmt.Printf("employessSalary[%s] = %d\n", key, value)
}
5. Deleting items from a map
employeeSalary := map[string]int{
"steve": 12000,
"jamie": 15000,
"mike": 9000,
}
fmt.Println("Contents of the map")
for key, value := range employeeSalary {
fmt.Printf("employessSalary[%s] = %d\n", key, value)
}
6.Map of structs
package main
import (
"fmt"
)
type employee struct {
salary int
country string
}
func main() {
emp1 := employee{salary: 6666, country: "Usa"}
emp2 := employee{salary: 7777, country: "Canada"}
emp3 := employee{salary: 8888, country: "China"}
employeeInfo := map[string]employee{
"Steve": emp1,
"Lisa": emp2,
"Like": emp3,
}
for name, info := range employeeInfo {
fmt.Printf("Employee: %s Salary:$%d Country: %s\n", name, info.salary, info.country)
}
}
7.Length of the map
emp1 := employee{salary: 6666, country: "Usa"}
emp2 := employee{salary: 7777, country: "Canada"}
emp3 := employee{salary: 8888, country: "China"}
employeeInfo := map[string]employee{
"Steve": emp1,
"Lisa": emp2,
"Like": emp3,
}
fmt.Println("length is", len(employeeInfo))
8.Maps are reference types
employeeSalary := map[string]int{
"steve": 12000,
"jamie": 15000,
"mike": 9000,
}
fmt.Println("Original employee salary", employeeSalary)
modified := employeeSalary
modified["mike"] = 18000
fmt.Println("Employee salary changed", employeeSalary)
9.Maps equality
package main
func main() {
map1 := map[string]int{
"one": 1,
"two": 2,
}
map2 := map1
if map1 == map2 {
}
}
三、Strings
1.Accessing individual bytes of a string
package main
import "fmt"
func printBytes(s string) {
fmt.Printf("Bytes:")
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
}
func main() {
name := "Hello World"
fmt.Printf("String: %s\n", name)
printBytes(name)
}
2.Accessing individual characters of a string
package main
import "fmt"
func printBytes(s string) {
fmt.Printf("Bytes:")
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
}
func printChars(s string) {
fmt.Printf("Characters: ")
runes := []rune(s)
for i := 0; i < len(runes); i++ {
fmt.Printf("%c", runes[i])
}
}
func main() {
name := "Hello World"
fmt.Printf("String: %s\n", name)
printChars(name)
fmt.Printf("\n")
printBytes(name)
fmt.Printf("\n\n")
name = "Señor"
fmt.Printf("String: %s\n", name)
printChars(name)
fmt.Printf("\n")
printBytes(name)
}
3.String length
word1 := "Señor"
fmt.Printf("String: %s\n", word1)
fmt.Printf("Length: %d\n", utf8.RuneCountInString(word1))
fmt.Printf("Number of bytes: %d\n", len(word1))
fmt.Printf("\n")
word2 := "Pets"
fmt.Printf("String: %s\n", word2)
fmt.Printf("Length: %d\n", utf8.RuneCountInString(word2))
fmt.Printf("Number of bytes: %d\n", len(word2))
4.String comparison
func compareStrings(str1 string, str2 string) {
if str1 == str2 {
fmt.Printf("%s and %s are equal\n", str1, str2)
return
}
fmt.Printf("%s and %s are not equal\n", str1, str2)
}
string1 := "Go"
string2 := "Go"
compareStrings(string1, string2)
string3 := "hello"
string4 := "world"
compareStrings(string3, string4)
5.String concatenation
string1 := "Go"
string2 := "is awesome"
result := fmt.Sprintf("%s %s", string1, string2)
fmt.Println(result)
6.Strings are immutable
func mutate(s string)string {
s[0] = 'a'
return s
}
func main() {
h := "hello"
fmt.Println(mutate(h))
}
func mutate(s []rune) string {
s[0] = 'a'
return string(s)
}
func main() {
h := "hello"
fmt.Println(mutate([]rune(h)))
}
四、Printf的%格式化字符串
%d: 表示有符号十进制整数(int类型)
%f: 表示浮点数(float32或float64类型)
%s: 表示字符串
%t: 表示布尔值(true或false)
%c: 表示字符(按Unicode码点输出)
%p: 表示指针地址
%v: 表示通用的值(以默认方式格式化)
%+v: 表示结构体值(包含字段名)
%#v: 表示值的Go语法表示形式
%x: 表示以十六进制格式输出整数或字节切片
%b: 表示以二进制格式输出整数
%o: 表示以八进制格式输出整数
%e 或 %E: 表示科学计数法表示的浮点数
%t: 表示以字符串形式输出时间(time.Time类型)
%T: 表示值的类型(类型的完全规格)
%10d: 表示输出宽度为10的有符号十进制整数
%.2f: 表示输出带有两位小数的浮点数
%05d: 表示输出宽度为5的有符号十进制整数,左侧用零填充