Skip to content

Instantly share code, notes, and snippets.

@Liutos
Created October 26, 2012 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Liutos/3957747 to your computer and use it in GitHub Desktop.
Save Liutos/3957747 to your computer and use it in GitHub Desktop.
多重返回值的演示
(truncate 10 3) ;一般情况下返回值为3,即10除以3的商,余数被丢弃了。
(multiple-value-bind (q r)
(truncate 10 3)
(format t "~D and ~D" q r)) ;输出结果为"3 and 1"。truncate实际上是会返回
;多个值(即多重返回值)的一个函数,不过一般只会得
;到一个,所以要用宏multiple-value-bind来捕捉
;所有的返回值。相当于把(truncate 10 3)的第一个
;返回值赋值给了q,第二个赋值给了r。
@iwinux
Copy link

iwinux commented Oct 26, 2012

Go 语言里面的语法没有那么灵活,大概是像这样的:

func divide(num, divider float) (quotient, remainder int) {
  quotient = int(num / divider)
  remainder = int(num % divider)
  return
}

func main() {
  q, r := divide(10, 3) // 必须同时接收同等数量的返回值
  q, _ := divide(10, 3) // 可以用 _ 表示遗弃的返回值
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment