-
每个go程序都是由包构成
-
(1) 同一个目录xx下的所有go文件,开头的package xxx中的xxx必须相同
(然而神奇的是目录名称xx和package名xxx可以不相同)
(2) 程序总是从__main package__开始运行,package名不是main的go文件即便定义了func main()也没用
(3) import的是目录,调用的时候用的是包名
(4) 同一个目录下,如果package名是main的话,那么只能有一个func main;如果有多个func main,需要在前面加上,并且再空一行再开始其他内容,否则会报错
// +build ignore
(5) 不同目录下可以建立多个名为package main的func main,分别进行测试(因为go允许目录名和包名不一致,所以可以暂时把包名改成main,测完了再改回来)
-
导入import
(1) 可以使用多个import
import "fmt" import "math"
(2) 也可以使用分组导入,用括号把它们括起来(推荐)
import ( "fmt" "math" )
其中,fmt和math都是C:\Go\src下的包
-
已导出exported
(1) 如果一个名字以__大写字母__开头,就是__已导出的__
(2) 在导入一个包时,只能引用其中__已导出__的名字;
任何未导出的名字在该包外均无法访问,但是包内是自由访问的
package main import ( "fmt" "math" ) func main() { fmt.Println(math.Pi) //正确的写法 fmt.Println(math.pi) //会报错 }
-
注释
(1) 单行注释: //
(2) 多行注释: /* */
-
函数
(1) func开头
(2) 可以有0个, 1个或多个参数
(3) 类型在变量名之后
package main import "fmt" func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) }
(4) 当连续两个或多个函数的已命名形参类型相同时,除最后一个类型以外,其它都可以省略
package main import "fmt" func add(x, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) }
(5) 函数可以返回__任意数量的返回值__
函数返回值类型写在参数后面,大括号前面
package main import "fmt" func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("hello", "world") fmt.Println(a, b) }
(6) 返回值可以被命名,作为__文档__使用
package main import "fmt" func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return x, y } func main() { fmt.Println(split(17)) }
(7) 比较特殊的是error接口类型也作为返回值,类似于抛出异常
-
变量
(1) var用来声明一个变量(列表)
(2) 和函数参数列表、返回值列表一样,声明的变量类型放在最后
(3) var可以出现在包级别或函数级别
package main import "fmt" var c, python, java bool func main() { var i int fmt.Println(i, c, python, java) }
(4) 变量可以赋初值,如果赋初值的话可以不用指定变量类型
package main import "fmt" var i, j int = 1, 2 var k = "haha" func main() { var c, python, java = true, false, "no!" fmt.Println(i, j, c, python, java) fmt.Println(k) }
(5) 在函数中,可以__用:=代替var__进行变量简短声明,但是要在类型明确的地方
函数外声明变量只能用var
package main import "fmt" func main() { var i, j int = 1, 2 k := 3 c, python, java := true, false, "no!" fmt.Println(i, j, k, c, python, java) }
-
基本数据类型
(1) bool
(2) string
(3) int int8 int16 int32 int64
(4) uint uint8 uint16 uint32 uint64 uintptr
(5) byte // uint8 的别名
(6) rune // int32 的别名 // 表示一个 Unicode 码点
(7) float32 float64
(8) complex64 complex128
package main import ( "fmt" "math/cmplx" ) var ( //声明变量也可以使用分组形式 ToBe bool = false MaxInt uint64 = 1<<64 - 1 z complex128 = cmplx.Sqrt(-5 + 12i) ) func main() { fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe) fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt) fmt.Printf("Type: %T Value: %v\n", z, z) }
-
基本数据的__零值__
(1) 没有明确初始值的变量声明会被赋予它们的零值
(2) 数值类型为 0,布尔类型为 false,字符串为 “”(空字符串),数组、接口之类的是nil
-
(1) Go 在不同类型的项之间赋值时需要__显式转换__,无论是怎么转
(2) 可以靠var声明的方式转,也可以靠:=的方式转
package main import ( "fmt" "math" ) func main() { var x, y int = 3, 4 var f float64 = math.Sqrt(float64(x*x + y*y)) var z uint = uint(f) i := 42 g := float64(i) u := uint(f) fmt.Println(x, y, z, i, g, u) }
(3) 不显式指定变量类型时,可以自动进行__类型推导__
package main import "fmt" func main() { v := 42.2 + 0.8i // 修改这里! fmt.Printf("v is of type %T\n", v) }
-
常量
(1) 使用 const 关键字声明
(2) 常量可以是字符、字符串、布尔值或数值
(3) 常量__不能__用 := 语法声明
package main import "fmt" const Pi = 3.14 func main() { const World = "世界" fmt.Println("Hello", World) fmt.Println("Happy", Pi, "Day") const Truth = true fmt.Println("Go rules?", Truth) }
(4) 一个未指定类型的常量由上下文来决定类型
package main import "fmt" const ( // 将 1 左移 100 位来创建一个非常大的数字 // 即这个数的二进制是 1 后面跟着 100 个 0 Big = 1 << 100 // 再往右移 99 位,即 Small = 1 << 1,或者说 Small = 2 Small = Big >> 99 ) func needInt(x int) int { return x*10 + 1 } func needFloat(x float64) float64 { return x * 0.1 } func main() { fmt.Println(needInt(Small)) //fmt.Println(needInt(Big)) //会报错 fmt.Println(needFloat(Small)) fmt.Println(needFloat(Big)) }
1_Packages_Variables_And_Functions
最新推荐文章于 2021-12-30 12:03:49 发布