gpt4 book ai didi

go - 为什么 %T 不打印常量的类型?

转载 作者:IT王子 更新时间:2023-10-29 00:51:55 25 4
gpt4 key购买 nike

我只是在使用官方导览/教程学习 golang。在其中一个示例中,我看到一条注释说 An untyped constant takes the type needed by its context.

我正在尝试这个:

package main

import "fmt"

const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
)

func main() {
fmt.Printf("Big is of type %T\n", Big)
}

但是当我运行它时失败了:

# command-line-arguments
./compile63.go:12:13: constant 1267650600228229401496703205376 overflows int

为什么我无法通过这种方式发现常量的类型? (请注意,我完全是个菜鸟,很可能还没有充分了解该语言,无法自己解决这个问题)。

最佳答案

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.


The Go Programming Language Specification


Variable declarations

A variable declaration creates one or more variables, binds corresponding identifiers to them, and gives each a type and an initial value.

If a list of expressions is given, the variables are initialized with the expressions following the rules for assignments. Otherwise, each variable is initialized to its zero value.

If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment. If that value is an untyped constant, it is first converted to its default type; if it is an untyped boolean value, it is first converted to type bool. The predeclared value nil cannot be used to initialize a variable with no explicit type.


Constants

Constants may be typed or untyped. Literal constants, true, false, iota, and certain constant expressions containing only untyped constant operands are untyped.

A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type.

An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.


Numeric types

A numeric type represents sets of integer or floating-point values. Some predeclared architecture-independent numeric types:

int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

There are also some predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int same size as uint

Big 是一个无类型常量。没有类型可以发现。当在变量或赋值中使用时,它被赋予一个类型。无类型常量 Big 的默认类型是 int

const Big = 1 << 100

在 Go 中,所有参数都像赋值一样按值传递。对于 fmt.Printf,第二个参数是 interface{} 类型。因此,等价地,

var arg2 interface{} = Big  // constant 1267650600228229401496703205376 overflows int
fmt.Printf("Big is of type %T\n", arg2)

无类型整型常量的默认类型是int。溢出是编译时错误。


例如,

package main

import "fmt"

const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
)

func main() {
var arg2 interface{} = Big
fmt.Printf("Big is of type %T\n", arg2)

fmt.Printf("Big is of type %T\n", Big)
}

Playground :https://play.golang.org/p/9tynPTek3wN

输出:

prog.go:12:6: constant 1267650600228229401496703205376 overflows int
prog.go:15:13: constant 1267650600228229401496703205376 overflows int

引用:The Go Blog: Constants

关于go - 为什么 %T 不打印常量的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com