Modified some sentence.
This commit is contained in:
216
2.3.md
216
2.3.md
@@ -8,18 +8,18 @@ if语法也许是所有语言里面最常见的一种语法了,它的语法概
|
||||
Go里面`if`条件语法中不需要括号,如下代码所示
|
||||
|
||||
if x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
Go的`if`还有一个强大的地方就是条件里面允许声明一个变量,这个变量的作用域只能在该条件中,出了这个条件就不起作用了,如下所示
|
||||
|
||||
// 计算获取值x,然后根据x返回的大小,判断是否大于10.
|
||||
if x := computedValue(); x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
//这个地方如果这样调用就编译出错了,因为x是条件里面的变量
|
||||
@@ -30,9 +30,9 @@ Go的`if`还有一个强大的地方就是条件里面允许声明一个变量
|
||||
if integer == 3 {
|
||||
fmt.Println("The integer is equal to 3")
|
||||
} else if integer < 3 {
|
||||
fmt.Println("The integer is less than 3")
|
||||
fmt.Println("The integer is less than 3")
|
||||
} else {
|
||||
fmt.Println("The integer is greater than 3")
|
||||
fmt.Println("The integer is greater than 3")
|
||||
}
|
||||
|
||||
###goto
|
||||
@@ -53,7 +53,7 @@ Go有`goto`语句——请明智地使用它。用`goto`跳转到一定是当前
|
||||
Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读取数据,又可以当作`while`来控制逻辑,还能迭代操作。它的语法如下
|
||||
|
||||
for expression1; expression2; expression3 {
|
||||
...
|
||||
...
|
||||
}
|
||||
|
||||
`expression1`、`expression2`和`expression3`都是表达式,其中`expression1`和`expression3`是变量声明或者函数调用返回值之类的,`expression2`是条件判断,`expression1`在循环开始之前调用,`expression3`在每轮循环结束之时调用。
|
||||
@@ -64,11 +64,11 @@ Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读
|
||||
import "fmt"
|
||||
|
||||
func main(){
|
||||
sum := 0;
|
||||
for index:=0; index < 10 ; index++ {
|
||||
sum += index
|
||||
}
|
||||
fmt.Println("sum is equal to ", sum)
|
||||
sum := 0;
|
||||
for index:=0; index < 10 ; index++ {
|
||||
sum += index
|
||||
}
|
||||
fmt.Println("sum is equal to ", sum)
|
||||
}
|
||||
//输出:sum is equal to 45
|
||||
|
||||
@@ -79,23 +79,23 @@ Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读
|
||||
|
||||
sum := 1
|
||||
for ; sum < 1000; {
|
||||
sum += sum
|
||||
sum += sum
|
||||
}
|
||||
|
||||
其中`;`也可以省略,那么就变成如下的代码了,是不是似曾相识,对,这就是`while`的功能
|
||||
|
||||
sum := 1
|
||||
for sum < 1000 {
|
||||
sum += sum
|
||||
sum += sum
|
||||
}
|
||||
|
||||
在循环里面有两个关键操作`break`和`continue` ,`break`操作是跳出当前循环,`continue`是跳出本次循环,当嵌套过深的时候,`break`可以配合标签使用,即跳出标签所指定的循环,详细参考如下例子
|
||||
|
||||
for index := 10; index>0; index-- {
|
||||
if index == 5{
|
||||
break或者continue
|
||||
}
|
||||
fmt.Println(index)
|
||||
if index == 5{
|
||||
break或者continue
|
||||
}
|
||||
fmt.Println(index)
|
||||
}
|
||||
//break打印出来10、9、8、7、6
|
||||
//continue打印出来10、9、8、7、6、4、3、2、1
|
||||
@@ -116,51 +116,51 @@ Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读
|
||||
有些时候你需要写很多的`if/else`来实现一些逻辑处理,这个时候代码看上去就很丑很冗长,而且也不易于以后的维护,这个时候`switch`就能很好的解决这个问题,它的语法如下
|
||||
|
||||
switch sExpr {
|
||||
case expr1:
|
||||
some instructions
|
||||
case expr2:
|
||||
some other instructions
|
||||
case expr3:
|
||||
some other instructions
|
||||
default:
|
||||
other code
|
||||
case expr1:
|
||||
some instructions
|
||||
case expr2:
|
||||
some other instructions
|
||||
case expr3:
|
||||
some other instructions
|
||||
default:
|
||||
other code
|
||||
}
|
||||
|
||||
`sExpr`和`expr1`、`expr2`、`expr3`的类型必须一致。Go的`switch`非常灵活。表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项,而如果`switch`没有表达式,它会匹配`true`。
|
||||
|
||||
i := 10
|
||||
switch i {
|
||||
case 1:
|
||||
fmt.Println("i is equal to 1")
|
||||
case 2, 3, 4:
|
||||
fmt.Println("i is equal to 2, 3 or 4")
|
||||
case 10:
|
||||
fmt.Println("i is equal to 10")
|
||||
default:
|
||||
fmt.Println("All I know is that i is an integer")
|
||||
case 1:
|
||||
fmt.Println("i is equal to 1")
|
||||
case 2, 3, 4:
|
||||
fmt.Println("i is equal to 2, 3 or 4")
|
||||
case 10:
|
||||
fmt.Println("i is equal to 10")
|
||||
default:
|
||||
fmt.Println("All I know is that i is an integer")
|
||||
}
|
||||
|
||||
我们看第六行,我们把很多值聚合在了一个`case`里面,同时,Go里面`switch`默认相当于每个`case`后面带有`break`,匹配成功后不会自动向下尝试,而是跳出整个`switch`了,但是可以使用`fallthrough`使其这样做。
|
||||
在第5行中,我们把很多值聚合在了一个`case`里面,同时,Go里面`switch`默认相当于每个`case`后面带有`break`,匹配成功后不会自动向下尝试,而是跳出整个`switch`了,但是可以使用`fallthrough`使其这样做。
|
||||
|
||||
integer := 6
|
||||
switch integer {
|
||||
case 4:
|
||||
fmt.Println("The integer was <= 4")
|
||||
fallthrough
|
||||
fmt.Println("The integer was <= 4")
|
||||
fallthrough
|
||||
case 5:
|
||||
fmt.Println("The integer was <= 5")
|
||||
fallthrough
|
||||
fmt.Println("The integer was <= 5")
|
||||
fallthrough
|
||||
case 6:
|
||||
fmt.Println("The integer was <= 6")
|
||||
fallthrough
|
||||
fmt.Println("The integer was <= 6")
|
||||
fallthrough
|
||||
case 7:
|
||||
fmt.Println("The integer was <= 7")
|
||||
fallthrough
|
||||
fmt.Println("The integer was <= 7")
|
||||
fallthrough
|
||||
case 8:
|
||||
fmt.Println("The integer was <= 8")
|
||||
fallthrough
|
||||
fmt.Println("The integer was <= 8")
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Println("default case")
|
||||
fmt.Println("default case")
|
||||
}
|
||||
|
||||
上面的程序将输出
|
||||
@@ -175,9 +175,9 @@ Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读
|
||||
函数是Go里面的核心设计,它通过关键字`func`来声明,它的格式如下
|
||||
|
||||
func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) {
|
||||
//这里是处理逻辑代码
|
||||
//返回多个值
|
||||
return value1, value2
|
||||
//这里是处理逻辑代码
|
||||
//返回多个值
|
||||
return value1, value2
|
||||
}
|
||||
|
||||
上面的代码我们看出
|
||||
@@ -196,23 +196,23 @@ Go里面最强大的一个控制逻辑就是`for`,它即可以用来循环读
|
||||
|
||||
//返回a、b中最大值.
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
y := 4
|
||||
z := 5
|
||||
x := 3
|
||||
y := 4
|
||||
z := 5
|
||||
|
||||
max_xy := max(x, y) //调用函数max(x, y)
|
||||
max_xz := max(x, z) //调用函数max(x, z)
|
||||
max_xy := max(x, y) //调用函数max(x, y)
|
||||
max_xz := max(x, z) //调用函数max(x, z)
|
||||
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, y, max_xy)
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, z, max_xz)
|
||||
fmt.Printf("max(%d, %d) = %d\n", y, z, max(y,z)) //just call it here
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, y, max_xy)
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, z, max_xz)
|
||||
fmt.Printf("max(%d, %d) = %d\n", y, z, max(y,z)) //just call it here
|
||||
}
|
||||
|
||||
上面这个里面我们可以看到`max`函数有两个参数,它们的类型都是`int`,那么第一个变量的类型可以省略,默认为离它最近的类型,同理多于2个同类型的变量或者返回值。同时我们注意到它的返回值就是一个类型,这个就是省略写法。
|
||||
@@ -227,23 +227,23 @@ Go语言和C相比,更先进的地方,其中一点就是能够返回多个
|
||||
|
||||
//返回 A+B 和 A*B
|
||||
func SumAndProduct(A, B int) (int, int) {
|
||||
return A+B, A*B
|
||||
return A+B, A*B
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
y := 4
|
||||
x := 3
|
||||
y := 4
|
||||
|
||||
xPLUSy, xTIMESy := SumAndProduct(x, y)
|
||||
xPLUSy, xTIMESy := SumAndProduct(x, y)
|
||||
|
||||
fmt.Printf("%d + %d = %d\n", x, y, xPLUSy)
|
||||
fmt.Printf("%d * %d = %d\n", x, y, xTIMESy)
|
||||
fmt.Printf("%d + %d = %d\n", x, y, xPLUSy)
|
||||
fmt.Printf("%d * %d = %d\n", x, y, xTIMESy)
|
||||
}
|
||||
|
||||
上面的例子我们可以看到直接返回了两个参数,当然我们也可以命名返回参数的变量,这个例子里面只是用了两个类型,我们也可以改成如下这样的定义,然后返回的时候不用带上变量名,因为直接在函数里面初始化了。但是当你的函数如果是导出的(首字母大写),官方建议,不要命名返回值名称,因为这样会造成生成的文档不易读。
|
||||
|
||||
func SumAndProduct(A, B int) (add int, Multiplied int) {
|
||||
add = A+B
|
||||
add = A+B
|
||||
Multiplied = A*B
|
||||
return
|
||||
}
|
||||
@@ -268,19 +268,19 @@ Go函数支持变参。接受变参的函数是有着不定数量的参数的。
|
||||
|
||||
//简单的一个函数,实现了参数+1的操作
|
||||
func add1(a int) int {
|
||||
a = a+1 // 我们改变了a的值
|
||||
return a //返回一个新值
|
||||
a = a+1 // 我们改变了a的值
|
||||
return a //返回一个新值
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
x := 3
|
||||
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 3"
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 3"
|
||||
|
||||
x1 := add1(x) //调用add1(x)
|
||||
x1 := add1(x) //调用add1(x)
|
||||
|
||||
fmt.Println("x+1 = ", x1) // 应该输出"x+1 = 4"
|
||||
fmt.Println("x = ", x) // 应该输出"x = 3"
|
||||
fmt.Println("x+1 = ", x1) // 应该输出"x+1 = 4"
|
||||
fmt.Println("x = ", x) // 应该输出"x = 3"
|
||||
}
|
||||
|
||||
看到了吗?虽然我们调用了`add1`函数,并且在`add1`中执行`a=a+1`操作,但是上面例子中`x`变量的值没有发生变化
|
||||
@@ -296,19 +296,19 @@ Go函数支持变参。接受变参的函数是有着不定数量的参数的。
|
||||
|
||||
//简单的一个函数,实现了参数+1的操作
|
||||
func add1(a *int) int { // 请注意,
|
||||
*a = *a+1 // 修改了a的值
|
||||
return *a // 返回新值
|
||||
*a = *a+1 // 修改了a的值
|
||||
return *a // 返回新值
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
x := 3
|
||||
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 3"
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 3"
|
||||
|
||||
x1 := add1(&x) // 调用 add1(&x) 传x的地址
|
||||
x1 := add1(&x) // 调用 add1(&x) 传x的地址
|
||||
|
||||
fmt.Println("x+1 = ", x1) // 应该输出 "x+1 = 4"
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 4"
|
||||
fmt.Println("x+1 = ", x1) // 应该输出 "x+1 = 4"
|
||||
fmt.Println("x = ", x) // 应该输出 "x = 4"
|
||||
}
|
||||
|
||||
这样,我们就达到了修改`x`的目的。那么到底传指针有什么好处呢?
|
||||
@@ -354,7 +354,7 @@ Go里面有一个不错的设计,就是回调函数,有点类似面向对象
|
||||
如果有很多调用`defer`,那么`defer`是采用后进先出模式,所以如下代码会输出`4 3 2 1 0`
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
defer fmt.Printf("%d ", i)
|
||||
defer fmt.Printf("%d ", i)
|
||||
}
|
||||
|
||||
###函数作为值、类型
|
||||
@@ -371,37 +371,37 @@ Go里面有一个不错的设计,就是回调函数,有点类似面向对象
|
||||
type testInt func(int) bool //声明了一个函数类型
|
||||
|
||||
func isOdd(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
if integer%2 == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isEven(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
if integer%2 == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//声明的函数类型在这个地方当做了一个参数
|
||||
//声明的函数类型在这个地方当做了一个参数
|
||||
func filter(slice []int, f test_int) []int {
|
||||
var result []int
|
||||
for _, value := range slice {
|
||||
if f(value) {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
var result []int
|
||||
for _, value := range slice {
|
||||
if f(value) {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main(){
|
||||
slice := []int {1, 2, 3, 4, 5, 7}
|
||||
fmt.Println("slice = ", slice)
|
||||
odd := filter(slice, isOdd) //函数当做值来传递了
|
||||
fmt.Println("Odd elements of slice are: ", odd)
|
||||
even := filter(slice, isEven)//函数当做值来传递了
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
slice := []int {1, 2, 3, 4, 5, 7}
|
||||
fmt.Println("slice = ", slice)
|
||||
odd := filter(slice, isOdd) //函数当做值来传递了
|
||||
fmt.Println("Odd elements of slice are: ", odd)
|
||||
even := filter(slice, isEven)//函数当做值来传递了
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
}
|
||||
|
||||
函数当做值和类型在我们写一些通用接口的时候非常有用,通过上面例子我们看到`testInt`这个类型是一个函数类型,然后两个`filter`函数的参数和返回值与`testInt`类型是一样的,但是我们可以实现很多种的逻辑,这样使得我们的程序变得非常的灵活。
|
||||
@@ -421,9 +421,9 @@ Recover
|
||||
var user = os.Getenv("USER")
|
||||
|
||||
func init() {
|
||||
if user == "" {
|
||||
panic("no value for $USER")
|
||||
}
|
||||
if user == "" {
|
||||
panic("no value for $USER")
|
||||
}
|
||||
}
|
||||
|
||||
下面这个函数检查作为其参数的函数在执行时是否会产生`panic`:
|
||||
|
||||
Reference in New Issue
Block a user