在tour.golang.org/moretypes/18中,dx和dy的值是256。

huangapple go评论69阅读模式
英文:

How is the value of dx and dy 256 in tour.golang.org/moretypes/18?

问题

我一直在使用tour.golang.org学习golang。

在下面的代码中,当我打印类型为整数的dx和dy的值时,值为256。

package main

import (
	"golang.org/x/tour/pic"
	"fmt"
)

func Pic(dx, dy int) [][]uint8 {
	fmt.Printf("%T\n", dx)
	fmt.Println(dx)

	ret := make([][]uint8, dy)
	for i := range ret {
		ret[i] = make([]uint8, dx)
	}
	return ret
}

func main() {
	pic.Show(Pic)
}

golang编译器是如何知道在函数Pic中声明的这两个整数的值的呢?

英文:

I've been learning golang using tour.golang.org.

In the following code, when I print the value of dx and dy which are both of type integer, the value comes out to be 256.

package main

import ("golang.org/x/tour/pic"
		"fmt"
		)

func Pic(dx, dy int) [][]uint8 {
	fmt.Printf("%T\n",dx)
	fmt.Println(dx)
	
	ret := make([][]uint8, dy)
    for i := range(ret){
		ret[i] = make([]uint8, dx)
	}
	return ret
}

func main() {
	pic.Show(Pic)
}

How does the golang compiler know the value of those two integers declared in the function Pic?

答案1

得分: 0

main函数中,你正在调用pic.Show函数,并传入你的函数Pic作为参数。查看pic.Show源代码,可以看到它将每个维度都设置为256,并调用了传入的函数。

英文:

From main, you're calling pic.Show with your function, Pic. Looking at the source code for pic.Show, it calls the function it's given with 256 for each dimension.

huangapple
  • 本文由 发表于 2021年8月4日 22:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68653195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定