头图

background

Google engineer Valentin Deleplace came up with a few calculation questions about Go numerical types, which are very confusing. The overall correct rate is less than 50%. I will share them with you.

Topic 1

 var y = 5.2
const z = 2
fmt.Println(y / z)
  • A: 2.6
  • B: 2.5
  • C: 2
  • D: compile error

Topic 2

 const a = 7.0
var b = 2
fmt.Println(a / b)
  • A: 3.5
  • B: 3
  • C: compile error

topic 3

 a := 40
f := float64(a/100.0)
fmt.Println(f)
  • A: 0
  • B: 0.4
  • C: compile error

Parse

This question mainly examines 3 knowledge points:

  • For variables, if the specified data type is not displayed, the compiler will automatically deduce the determined data type according to the assignment.

    The default type of integer is int , and the default type of floating point number is float64 . The official description is as follows:

    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.
  • For constants, if the specified data type is not displayed , the compiler will also deduce a data type, but the constant without the specified data type can be implicitly converted to the required data type for calculation in the code context .
  • Go does not allow operations on different data types. When a variable is mixed with a constant that does not display the specified data type , if the constant is converted to the variable type without losing precision, the constant will be automatically converted to the variable data type to participate in the operation. If the constant is converted into a variable type that loses precision, it will compile an error.

For topic 1: The variable y does not display the specified data type, but according to the subsequent assignment 5.2 , the compiler automatically deduces the data type of the variable y . z类型,编译器自动推导出的类型是inty/z时, y It is of type float64, z converting to float64 type will not lose precision, so z will be automatically converted to float64 type during operation, so the operation result of this question is 2.6 , the answer is A .

For topic 2: The variable b does not display the specified data type. According to the following assignment 2 , the compiler automatically deduces the data type of the variable b as int. The constant a does not display the specified data type, the type automatically deduced by the compiler is float64, but in the operation a/b , because b is an int type, a Converting to int type will not lose precision, so a will be automatically converted to int type to participate in the calculation, so the result of this question is 7/2 3 the result is 3 , the answer is B .

For topic 3: The variable a does not display the specified data type. According to the subsequent assignment 40 , the compiler automatically deduces the data type of the variable a is int. The constant 100.0 does not display the specified data type, the type automatically deduced by the compiler is float64, but when calculating a/100.0 , because a is an int type, 100.0 Converting to int type will not lose precision, so 100.0 will be automatically converted to int type to participate in the calculation, so the result of this question is 40/100 , the result 0 , the answer is A .

thinking questions

Topic 1:

 var (
    a = 1.0
    b = 2
)
fmt.Println(a / b)

Topic 2:

 const (
    x = 5.0
    y = 4
)
fmt.Println(x / y)

Topic 3:

 const t = 4.8
var u = 2
fmt.Println(t / u)

If you want to know the answer, you can send a message to the data to get the answer.

Summarize

  • For variables whose data type is not specified, the compiler will automatically deduce the default data type. When participating in the operation, the variable will always use the deduced data type to participate in the operation without any implicit type conversion.
  • For constants with no specified data type, although the compiler will automatically deduce the default data type, when participating in the operation, the constant can be automatically and implicitly converted to the required data type according to the context of the code, as long as there is no loss of precision That's it. If there is a loss of precision, it will compile an error.

open source address

Articles and sample code are open sourced on GitHub: Beginner, Intermediate, and Advanced Tutorials in Go .

Official account: coding advanced. Follow the official account to get the latest Go interview questions and technology stacks.

Personal website: Jincheng's Blog .

Zhihu: Wuji .

References


coding进阶
116 声望18 粉丝