32 lines
480 B
Go
32 lines
480 B
Go
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// 基本函数定义
|
||
|
func greet(name string) {
|
||
|
fmt.Printf("Hello, %s!\n", name)
|
||
|
}
|
||
|
|
||
|
// 带返回值的函数
|
||
|
func add(a, b int) int {
|
||
|
return a + b
|
||
|
}
|
||
|
|
||
|
// 多返回值函数
|
||
|
func swap(x, y string) (string, string) {
|
||
|
return y, x
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
// 调用无返回值函数
|
||
|
greet("Gopher")
|
||
|
|
||
|
// 调用单返回值函数
|
||
|
sum := add(3, 5)
|
||
|
fmt.Println("3 + 5 =", sum)
|
||
|
|
||
|
// 调用多返回值函数
|
||
|
a, b := swap("hello", "world")
|
||
|
fmt.Println(a, b)
|
||
|
}
|