package main
import("fmt")funcHello(a int, b ...int){
fmt.Println(a, b)// 666 [22 333 555]}funcmain(){
Hello(666,22,333,555)}
2.可変引数関数の例と仕組みの理解
funcfind(num int, nums ...int){
// 设置一个目标数字与一个 Arrays 查看当前目标数字是否在arrays里面
fmt.Printf("type of nums is %T\n", nums)
found :=falsefor i, v :=range nums {
// 循环 Arraysif v == num {
// 如果值等于 num
fmt.Println(num,"found at index", i,"in", nums)
found =true}}if!found {
fmt.Println(num,"not find in ", nums)}
fmt.Printf("\n")}funcmain(){
find(89,87,90,88,89)find(45,56,67,45,90,109)find(78,38,56,98)find(87)}// type of nums is []int// 89 found at index 3 in [87 90 88 89]// 还可以这样调用
nums :=[]int{
89,90,95}find(89, nums)// 如果这样调用则报错 无法运行 需要添加 ... .\variadic functions. go:33:11: cannot use nums (variable of type []int) as int value in argument to findfind(89, nums...)// 89 found at index 0 in [89 90 95]
3.スライス引数と可変引数では変数パラメーターのみが変更されます
funcfind(num int, nums []int){
// 设置一个目标数字与一个 Arrays 查看当前目标数字是否在arrays里面
fmt.Printf("type of nums is %T\n", nums)
found :=falsefor i, v :=range nums {
// 循环 Arraysif v == num {
// 如果值等于 num
fmt.Println(num,"found at index", i,"in", nums)
found =true}}if!found {
fmt.Println(num,"not find in ", nums)}
fmt.Printf("\n")}funcmain(){
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")funcchange(s ...string){
s[0]="Go"
s =append(s,"playground")// append会创建一个新的切片
fmt.Println(s)// [Go world playground]}funcmain(){
welcome :=[]string{
"hello","world"}change(welcome...)// change没有受到添加playground的影响 所以它的容量还是2(change操作的是原始的切片, 如果原始切片满了则会创建一个新的切片)
fmt.Println(welcome)// 所以输出的是 Go world}// [Go world playground]// [Go world]// Go中的append原始切片如果没有满 则会使用原始切片不会创建新的切片 如果满了则会创建一个新的切片
2. 地図
1.マップを作成する
package main
import"fmt"// create a mapfuncmain(){
// example 1
employeeSalary :=make(map[string]int)
fmt.Println(employeeSalary)// example 2
Department :=map[string]string{
"Designer":"设计部","Research":"研发部",}
fmt.Println(Department)// add items to map
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// 不可行 panic: assignment to entry in nil map}
2.マップからキーの値を取得する
employeeSalary :=map[string]int{
"steve":12000,"jamie":15000,"mike":9000,}
employee :="jamie"
salary := employeeSalary[employee]
fmt.Println("Salary of", employee,"is", salary)// Salary of jamie is 15000
fmt.Println("Salary of mike is", employeeSalary["mike"])// Salary of mike is 9000
fmt.Println("Salary of joe is", employeeSalary["joe"])// Salary of joe is 0 没有则为0
3.キーが存在するかどうかを確認する
employeeSalary :=map[string]int{
"steve":12000,"jamie":15000,"mike":9000,}
newEmp :="jamie"
value, ok := employeeSalary[newEmp]// 0 false
fmt.Println(value, ok)if ok ==true{
fmt.Println(ok)// true
fmt.Println("Salary of", newEmp,"is", value)// Salary of jamie is 15000return}
fmt.Println(newEmp,"not Found")
4.マップ内のすべての要素を反復処理する
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)}// 输出// Contents of the map//employessSalary[steve] = 12000//employessSalary[jamie] = 15000//employessSalary[mike] = 9000
5. マップからアイテムを削除する
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)}// 输出// Contents of the map//employessSalary[steve] = 12000//employessSalary[jamie] = 15000//employessSalary[mike] = 9000
6.構造体のマップ
package main
import("fmt")type employee struct{
salary int
country string}funcmain(){
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)}}// Employee: Lisa Salary:$7777 Country: Canada// Employee: Like Salary:$8888 Country: China// Employee: Steve Salary:$6666 Country: Usa
funccompareStrings(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)// Go and Go are equal// hello and world are not equal
5.文字列の連結
string1 :="Go"
string2 :="is awesome"//result := string1 + " " + string2
result := fmt.Sprintf("%s %s", string1, string2)
fmt.Println(result)// Go is awesome
6.文字列は不変です
funcmutate(s string)string{
s[0]='a'//any valid unicode character within single quote is a rune return s
}funcmain(){
h :="hello"
fmt.Println(mutate(h))}// 输出 cannot assign to s[0] (value of type byte)// 优化funcmutate(s []rune)string{
// mutate函数接受一个[]rune类型的切片作为参数,并返回一个字符串。
s[0]='a'// []rune是一个Unicode字符的切片类型。它将字符串转换为Unicode字符切片,以便我们可以修改字符串中的字符。returnstring(s)}funcmain(){
h :="hello"
fmt.Println(mutate([]rune(h)))// mutate函数修改了切片中的第一个字符,将其替换为'a'。}