| 1234567891011121314151617181920212223242526 |
- package main
- import "fmt"
- func max(nums ...int) int {
- if len(nums) == 0 {
- panic("max: at least one argument required")
- }
- m := nums[0]
- for _, v := range nums[1:] {
- if v > m {
- m = v
- }
- }
- return m
- }
- func main() {
- fmt.Println(max(3, 7, 2, 9, 4)) // 9
- fmt.Println(max(-10, -5, -1, -100)) // -1
- fmt.Println(max(42))
- arr := []int{4, 5, 6}
- fmt.Println(max(arr...))
- }
|