mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
28
eBook/examples/chapter_6/varnumpar.go
Normal file
28
eBook/examples/chapter_6/varnumpar.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := Min(1, 3, 2, 0)
|
||||
fmt.Printf("The minimum is: %d\n", x)
|
||||
arr := []int{7,9,3,5,1}
|
||||
x = Min(arr...)
|
||||
fmt.Printf("The minimum in the array arr is: %d", x)
|
||||
}
|
||||
|
||||
func Min(a ...int) int {
|
||||
if len(a)==0 {
|
||||
return 0
|
||||
}
|
||||
min := a[0]
|
||||
for _, v := range a {
|
||||
if v < min {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
/*
|
||||
The minimum is: 0
|
||||
The minimum in the array arr is: 1
|
||||
*/
|
Reference in New Issue
Block a user