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)
}