【Go】累加器的测试问题记录

关于GoLang学习过程中的一个问题mark,教程上说两个累加器的地址应该是不一样的,但是实际测试出来结果一样

package main

import(
	"fmt"
)

func Accumulator(iValue int) func()int {
	return func() int {
		iValue++
		return iValue
	}
}
func main()  {
	fAccumulator := Accumulator(1)

	fmt.Println(fAccumulator())
	fmt.Println(fAccumulator())

	fmt.Printf("%p\n",fAccumulator)

	fAccumulator2 := Accumulator(10)

	fmt.Println(fAccumulator2())
	fmt.Println(fAccumulator2())

	fmt.Printf("%p\n",fAccumulator2)
	fmt.Println(fAccumulator())
}

  实际输出

PS E:\Data\01 Project\02 OpenSourceProj\06 Lang\02 Go\myGo\src\example_accumulator> go run .\main.go2
3
0x490810
11
12
0x490810
4

  

posted @ 2019-02-22 19:07  colourstar  阅读(330)  评论(0编辑  收藏  举报