go 基礎語法

guixiang發表於2024-10-27

go 基礎語法

結構

func function_name( [parameter list] ) [return_types] {
   函式體
}

示例

package main

import (
	"fmt"
)

func main(){
	a :=100
	var b=200
	var result int
	result = max(a,b)
	fmt.Printf("最大為 %d \n",result)

}
func max(c,d int) int {
	var res int
	if c>d {
		res = c
	}else {
		res =d
	}
	return res
}

函式返回多個值

package main
import "fmt"
func main(){
	name_one := "123"
	name_two := "456"
	var res string
	var out string
	res,out =swap(name_one,name_two)
	fmt.Println(res+out)
}
func swap(a,b string) (string,string){
	return b,a
}

變數作用域

//區域性變數
package main
import "fmt"
func main(){
	var a,b,c int
	a=1
	b=2
	c=a+b
	fmt.Println(c)
}
//全域性變數
package main
import "fmt"
var c int
func main(){
	var a,b int
	a=1
	b=2
	c=a+b
	fmt.Println(c)
}
//兩者優先順序
//區域性變數
package main
import "fmt"
	var a int =2
func main(){
	var a,b int
	var c int
	a=1
	b=2
	c=a+b
	fmt.Println(c)
}
//結果
[root@localhost tmp]# go run tmp3.go 
3
//形式引數
package main

import "fmt"

/* 宣告全域性變數 */
var a int = 20;

func main() {
   /* main 函式中宣告區域性變數 */
   var a int = 10
   var b int = 20
   var c int = 0

   fmt.Printf("main()函式中 a = %d\n",  a);
   c = sum( a, b);
   fmt.Printf("main()函式中 c = %d\n",  c);
}

/* 函式定義-兩數相加 */
func sum(a, b int) int {
   fmt.Printf("sum() 函式中 a = %d\n",  a);
   fmt.Printf("sum() 函式中 b = %d\n",  b);

   return a + b;
}
package main
import "fmt"
func main(){
	var number =[5] int {1,2,3,4,5}
	for j := 0; j < 5; j++ {
		number[j]=j+100
	}
	for i := 0; i < 5; i++ {
		fmt.Printf("number[%d]=%d\n",i,number[i])
	}
}

指標

package main
import "fmt"
func main(){
	var a string = "abc"
	var b *string
	b=&a
	fmt.Printf("a的地址為%x\n",&a)
	fmt.Printf("指標b指向%x\n",b)
	fmt.Printf("b=%s\n",*b)
}

指向陣列的指標

package main
import "fmt"
const MAX int =3
func main(){
	var number =[3] int{2,3,4}
	var str [MAX] *int
	var i int
	//正常輸出流程
	for i=0 ; i< MAX ; i++{
		fmt.Printf("number[%d]=%d\n",i,number[i])
	}
	//指標儲存
	for i := 0; i < MAX; i++ {
		str[i]=&number[i]
	}
	//指標輸出流程
	for i=0 ; i< MAX ; i++{
		fmt.Printf("number[%d]=%d\n",i,*str[i])
	}
}

指向指標的指標

package main
import "fmt"
func main(){
	var a int = 4
	var ptr *int
	var pptr **int
	ptr=&a
	pptr=&ptr
	fmt.Printf("pptr為%d\n",**pptr)
	fmt.Printf("ptr為%d\n",*ptr)
	fmt.Printf("ptr地址為%x\n",*pptr)
}

指標做函式引數

package main

import "fmt"

func main() {
	var a int = 10
	var b int = 20
	fmt.Printf("交換前\n a=%d,b=%d\n", a, b)
	swap(&a, &b)
	fmt.Printf("交換後\n a=%d,b=%d\n", a, b)
}
func swap(c, d *int) {
	var tmp int
	tmp = *c
	*c = *d
	*d = tmp

}

結構體

簡單定義與輸出

package main

import "fmt"

type book struct {
	title  string
	author string
	num    int
}

func main() {
	fmt.Printf("%v\n",book{"abc", "big", 12})
	fmt.Println(book{"Go 語言", "Go 語言教程", 6495407})
	fmt.Printf("%+v\n",book{"abc", "big", 12})

}

[root@localhost go]# ./tmp10 
{abc big 12}
{Go 語言 Go 語言教程 6495407}
{title:abc author:big num:12}
[root@localhost go]# 

訪問結構體成員

package main

import "fmt"

type student struct {
	name string
	id   int16
}

func main() {
	var xiaowang student
	xiaowang.name = "小王"
	xiaowang.id = 1
	xiaoming := student{"小明", 2}
	fmt.Println(xiaoming)
	fmt.Println(xiaowang)

}

結構體作為函式引數

package main

import "fmt"

type student struct {
	name string
	id   int16
}

func main() {
	var xiaowang student
	xiaowang.name = "小王"
	xiaowang.id = 1
	xiaoming := student{"小明", 2}
	PrintBook(xiaoming)
	PrintBook(xiaowang)

}
func PrintBook(student_info student){
	fmt.Printf("student_name: %s\n",student_info.name)
	fmt.Printf("student_id:%d\n",student_info.id)
}

結構體指標

package main

import "fmt"

type student struct {
	name string
	id   int16
}

func main() {
	var xiaowang student
	xiaowang.name = "小王"
	xiaowang.id = 1
	xiaoming := student{"小明", 2}
	PrintBook(&xiaoming)
	PrintBook(&xiaowang)

}
func PrintBook(student_info *student){
	fmt.Printf("student_name: %s\n",student_info.name)
	fmt.Printf("student_id:%d\n",student_info.id)
}

切片

一般寫法

package main

import "fmt"

func main() {
   /* 建立切片 */
   numbers := []int{0,1,2,3,4,5,6,7,8}   
   printSlice(numbers)

   /* 列印原始切片 */
   fmt.Println("numbers ==", numbers)

   /* 列印子切片從索引1(包含) 到索引4(不包含)*/
   fmt.Println("numbers[1:4] ==", numbers[1:4])

   /* 預設下限為 0*/
   fmt.Println("numbers[:3] ==", numbers[:3])

   /* 預設上限為 len(s)*/
   fmt.Println("numbers[4:] ==", numbers[4:])

   numbers1 := make([]int,0,5)
   printSlice(numbers1)
//numbers1 := make([]int,0,5) 建立了一個元素型別為 int 的切片,這個切片初始時沒有元素(長度為0),但具有5個元素的容量。
   /* 列印子切片從索引  0(包含) 到索引 2(不包含) */
   number2 := numbers[:2]
   printSlice(number2)

   /* 列印子切片從索引 2(包含) 到索引 5(不包含) */
   number3 := numbers[2:5]
   printSlice(number3)

}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

切片的複製與追加

package main

import "fmt"

func main() {
   var numbers []int
   printSlice(numbers)

   /* 允許追加空切片 */
   numbers = append(numbers, 0)
   printSlice(numbers)

   /* 向切片新增一個元素 */
   numbers = append(numbers, 1)
   printSlice(numbers)

   /* 同時新增多個元素 */
   numbers = append(numbers, 2,3,4)
   printSlice(numbers)

   /* 建立切片 numbers1 是之前切片的兩倍容量*/
   numbers1 := make([]int, len(numbers), (cap(numbers))*2)

   /* 複製 numbers 的內容到 numbers1 */
   copy(numbers1,numbers)
   printSlice(numbers1)   
}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

相關文章