mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 23:52:31 +08:00
添加06.2.md和相关的例子与问题解答
This commit is contained in:
15
eBook/examples/chapter_6/blank_identifier.go
Normal file
15
eBook/examples/chapter_6/blank_identifier.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var i1 int
|
||||
var f1 float32
|
||||
i1, _, f1 = ThreeValues()
|
||||
fmt.Printf("The int: %d, the float: %f \n", i1, f1)
|
||||
}
|
||||
|
||||
func ThreeValues() (int, int, float32) {
|
||||
return 5, 6, 7.5
|
||||
}
|
||||
|
20
eBook/examples/chapter_6/minmax.go
Normal file
20
eBook/examples/chapter_6/minmax.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var min, max int
|
||||
min, max = MinMax(78, 65)
|
||||
fmt.Printf("Minmium is: %d, Maximum is: %d\n", min, max)
|
||||
}
|
||||
|
||||
func MinMax(a int, b int) (min int, max int) {
|
||||
if a < b {
|
||||
min = a
|
||||
max = b
|
||||
} else { // a = b or a < b
|
||||
min = b
|
||||
max = a
|
||||
}
|
||||
return
|
||||
}
|
28
eBook/examples/chapter_6/multiple_return.go
Normal file
28
eBook/examples/chapter_6/multiple_return.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var num int = 10
|
||||
var numx2, numx3 int
|
||||
|
||||
func main() {
|
||||
numx2, numx3 = getX2AndX3(num)
|
||||
PrintValues()
|
||||
numx2, numx3 = getX2AndX3_2(num)
|
||||
PrintValues()
|
||||
}
|
||||
|
||||
func PrintValues() {
|
||||
fmt.Printf("num = %d, 2x num = %d, 3x num = %d\n", num, numx2, numx3)
|
||||
}
|
||||
|
||||
func getX2AndX3(input int) (int, int) {
|
||||
return 2 * input, 3 * input
|
||||
}
|
||||
|
||||
func getX2AndX3_2(input int) (x2 int, x3 int) {
|
||||
x2 = 2 * input
|
||||
x3 = 3 * input
|
||||
// return x2, x3
|
||||
return
|
||||
}
|
17
eBook/examples/chapter_6/side_effect.go
Normal file
17
eBook/examples/chapter_6/side_effect.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// this function changes reply:
|
||||
func Multiply(a, b int, reply *int) {
|
||||
*reply = a * b
|
||||
}
|
||||
|
||||
func main() {
|
||||
n := 0
|
||||
reply := &n
|
||||
Multiply(10, 5, reply)
|
||||
fmt.Println("Multiply:", *reply) // Multiply: 50
|
||||
}
|
15
eBook/examples/chapter_6/simple_function.go
Normal file
15
eBook/examples/chapter_6/simple_function.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Multiply 2 * 5 * 6 = %d\n", MultiPly3Nums(2, 5, 6))
|
||||
// var i1 int = MultiPly3Nums(2, 5, 6)
|
||||
// fmt.Printf("MultiPly 2 * 5 * 6 = %d\n", i1)
|
||||
}
|
||||
|
||||
func MultiPly3Nums(a int, b int, c int) int {
|
||||
// var product int = a * b * c
|
||||
// return product
|
||||
return a * b * c
|
||||
}
|
Reference in New Issue
Block a user