当前位置:网站首页>Multiple assignments in an introduction to go (using Fibonacci sequence as an example)

Multiple assignments in an introduction to go (using Fibonacci sequence as an example)

2020-11-13 03:11:58 Bit_ We

Go Language allows multiple variables to be assigned at one time .

Before actually updating variables , All the expressions on the right are deduced .

It can be done easily x,y Two are worth exchanging

x,y = y,x

  Fibonacci sequence :

package main

import "fmt"

func main() {
	fibonacci(6)
}
func fibonacci(n int) int{
	x,y :=0,1
	for i:=0;i<n;i++{
		x,y = y,x+y
		fmt.Println(x)
	}
	return x
}

Running results :

1
1
2
3
5
8

 

版权声明
本文为[Bit_ We]所创,转载请带上原文链接,感谢