Several cases go function return value of language

Three cases

(Hereinafter, "specifies a return value", that refers only to return back directly followed by the return value)

  • Withdraw, do not specify a return value
    (1) function does not return value
package main
import (
    "fmt"
) func GetMoney(){ fmt.Println("money") return } func main(){ GetMoney() } 

(2) function returns the value of the variable name

package main
import (
    "fmt"
) func GetMoney() (_amount int){ _amount = 88 fmt.Println("money: ",_amount) return } func main(){ var amount = GetMoney() fmt.Println("money: ",amount) } 
  • Exit execution, specify a return value
package main
import (
    "fmt"
) func GetMoney() (_amount int){ fmt.Println("money: ",_amount) return 88 } func main(){ var amount = GetMoney() fmt.Println("money: ",amount) } 运行结果: money: 0 money: 88 
  • Exit execution, specify the return value and specify a default value
package main
import (
    "fmt"
) func GetMoney() (_amount int){ _amount = 99 //如果return后面没有指定返回值,就用赋给“返回值变量”的值 fmt.Println("money: ",_amount) return 88 //如果return后面跟有返回值,就使用return后面的返回值 } func main(){ var amount = GetMoney() fmt.Println("money: ",amount) } 运行结果: money: 99 money: 88

 

Guess you like

Origin www.cnblogs.com/qiaoyanlin/p/12077152.html