Go语言的const

以下摘自The Go Programming Language

A const declaration gives names to constants, that is, values that are fixed at compile time. The value of a constant must be a number, string, or boolean.

const的值只能是数字,字符串或者布尔值。

 

A constant declaration may specify a type as well as a value, but in the absence of an explicit type, the type is inferred from the expression on the right-hand side.

当定义常量时没有指定其类型时,常量的类型将由等号右边的表达式来决定。

下面是如何定义一组constant

When a sequence of constants is declared as a group, the right-hand side expression may be omitted for all but the first of the group, implying that the previous expression and its type should be used again. For example:
const (
a = 1
b
c = 2
d
)
fmt.Println(a, b, c, d) // “1 1 2 2”

A const declaration may use the constant generator iota, which is used to create a sequence of related values without spelling out each one explicitly. In a const declaration, the value of iota begins at zero and increments by one for each item in the sequence. For example:
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
) This declares Sunday to be 0, Monday to be 1, and so on.

另外,关于untyped constants的定义:

Constants in Go are a bit unusual. Although a constant can have any of the basic data types like int or float64, including named basic types like time.Duration, many constants are not committed to a particular type. The compiler represents these uncommitted constants with much greater numeric precision than values of basic types, and arithmetic on them is more precise than machine arithmetic; you may assume at least 256 bits of precision. There are six flavors of these uncommitted constants, called untyped boolean, untyped integer, untyped rune, untyped floating-point, untyped complex, and untyped string.

只有constant才可能是untyped,变量不可能是untyped

Only constants can be untyped. When an untyped constant is assigned to a variable, as in the first statement below, or appears on the right-hand side of a variable declaration with an explicit type, as in the other three statements, the constant is implicitly converted to the type of that variable if possible.

var f float64 = 3 + 0i // untyped complex -> float64
f = 2 // untyped integer -> float64
f = 1e123 // untyped floating-point -> float64
f = ‘a’ // untyped rune -> float64
The statements above are thus equivalent to these:
var f float64 = float64(3 + 0i)
f = float64(2)
f = float64(1e123)
f = float64(‘a’)

constant之间转化要考虑是否有可能溢出:

Whether implicit or explicit, converting a constant from one type to another requires that the target type can represent the original value. Rounding is allowed for real and complex floating-point numbers:

const (
deadbeef = 0xdeadbeef // untyped int with value 3735928559
a = uint32(deadbeef) // uint32 with value 3735928559
b = float32(deadbeef) // float32 with value 3735928576 (rounded up)
c = float64(deadbeef) // float64 with value 3735928559 (exact)
d = int32(deadbeef) // compile error: constant overflows int32
e = float64(1e309) // compile error: constant overflows float64
f = uint(-1) // compile error: constant underflows uint )

要注意把untyped constant赋值给变量时,变量类型的选择。

In a variable declaration without an explicit type (including short variable declarations), the flavor of the untyped constant implicitly determines the default type of the variable, as in these examples:

i := 0 // untyped integer; implicit int(0)
r := ‘\000’ // untyped rune; implicit rune(‘\000’)
f := 0.0 // untyped floating-point; implicit float64(0.0)
c := 0i // untyped complex; implicit complex128(0i)

Note the asymmetry: untyped integers are converted to int, whose size is not guaranteed, but untyped floating-point and complex numbers are converted to the explicitly sized types float64 and complex128. The language has no unsized float and complex types analogous to unsized int, because it is very difficult to write correct numerical algorithms without knowing the size of one’s floating-point data types.

To give the variable a different type, we must explicitly convert the untyped constant to the desired type or state the desired type in the variable declaration, as in these examples:

var i = int8(0)
var i int8 = 0

These defaults are particularly important when converting an untyped constant to an interface value since they determine its dynamic type.

fmt.Printf(“%T\n”, 0) // “int”
fmt.Printf(“%T\n”, 0.0) // “float64”
fmt.Printf(“%T\n”, 0i) // “complex128”
fmt.Printf(“%T\n”, ‘\000’) // “int32” (rune)

 

 

Go语言的“++”和“—”运算符

以下摘自The Go Programming Language

The increment statement i++ adds 1 to i ; it’s equivalent to i += 1 which is in turn equivalent to i = i + 1 . There’s a corresponding decrement statement i– that subtracts 1. These are statements, not expressions as the y are in most languages in the C family, so j = i++ is illegal, and the y are postfix only, so –i is not legal either.

要注意,i++i--Go语言中是语句,不是表达式,因此不能赋值给另外的变量。此外没有++i--i

 

Go语言import语句的位置

以下摘自The Go Programming Language

The import declarations must follow the package declaration.After that, a program consists of the declarations of functions, variables, constants, and types (introduced by the key words func, var , const , and type ); for the most part, the order of declarations does not matter.

import语句必须跟在package定义后面。请看下例:

package main

var str string = "Hello world!\n"

import "fmt"

func main(){
    // your code goes 
    fmt.Println(str)
}

编译如下:

./prog.go:5: syntax error: unexpected import

调整一下语句位置:

package main

import "fmt"

var str string = "Hello world!\n"

func main(){
    // your code goes 
    fmt.Println(str)
}

编译执行成功:

Hello world!

 

Go语言的“main package”

以下摘自The Go Programming Language

Package main is special. It defines a standalone executable program, not a library. Within package main the function main is also special—it’s where execution of the program begins. Whatever main does is what the program does.

main package不同于其它library package,它定义了一个可执行程序。其中的main函数即是可执行文件的入口函数。