Old Shong Golang notes-variable declaration and initialization

Source: Internet
Author: User
Tags integer division
This is a creation in Article, where the information may have evolved or changed.

Variable declaration

Official doc:http://golang.org//spec#variable_declarations

Go uses the new keyword var to declare variables. var is not unfamiliar to us, it appears in both Javascript and C # . The difference is that variables in Go and C # are strongly typed, and they are not allowed to change their data types after declaring a variable.

There are several forms of declaring variables :

    1. var a int// Declare a variable of type int

    2. var b struct {// declares a struct

      Name string

      }

    3. var a = 8// declare variable assignment at the same time, the compiler automatically deduces its data type

    4. var a int = 8// declare variable assignment at the same time

    5. var {// batch declaration variable, concise

      a int

      B string

      }

Variable initialization

The initialization of a variable can be initialized when a variable is declared, or it can be declared after it is initialized. At this point the var keyword is no longer required.

There are several ways to initialize variables, each of which has a different usage scenario:

    1. Declare a temporary variable in the method and assign the initial value

      > var tmpstr = ""

      > var tmpstr string = ""

      > tmpstr: = ""

    2. Direct assignment of declared variables in the global

      > tmpstr = "<body>"

We see that there are two ways of doing this:

    1. var name [Type] = value

      If you do not write type , the type is automatically deduced at compile time based on value .

    2. Name: = value

The keyword varis omitted here, and I like this way (I can write less code without any harm). However, it is important to note that " : =" is the declaration and initialization of the variable, so the variable must be the first occurrence, the following initialization is wrong. However, it is important to be aware of the type you want when assigning, and it does not support implicit conversions in Go . If you are defining a variable of type float64 , write as v1: =8.0 instead of v1: =8 .

var a int

A: = 8

// when I first started with this error, ": =" is treated as a general assignment statement

Thinking questions

Initializes the statement on how the compiler makes automatic type deduction. How are some literal constants categorized?

such as 8→int, 8.0→float64

Package Main


Import (

"FMT"

"Reflect"

)


Func Main () {

var v1 int = 8

var v2 byte = 8

V3: = 8

v4: = 8.0

FMT. Printf ("v1 type:%s\n", reflect. TypeOf (v1))//int

FMT. Printf ("V2 type:%s\n", reflect. TypeOf (v2))//uint8

FMT. Printf ("V3 type:%s\n", reflect. TypeOf (v3))//int

FMT. Printf ("V4 type:%s\n", reflect. TypeOf (v4))//float64

}

Official document: http://golang.org/ref/spec#Constant_expressions

const A = 2 + 3.0//A = = 5.0 (untyped floating-point constant)Const B = 15/4//b = = 3 (untyped integer constant)Const C = 15/4.0//c = = 3.75 (untyped floating-point constant)Constθfloat64 = 3/2//θ== 1.0 (Type float64, 3/2 is integer division)Constπfloat64 = 3/2.//π== 1.5 (type float64, 3/2. is float division)Const D = 1 << 3.0//d = = 8 (untyped integer constant)Const E = 1.0 << 3//E = = 8 (untyped integer constant)Const F = int32 (1) <<//F = = 0 (type int32)Const G = float64 (2) >> 1//Illegal (float64 (2) is a typed floating-point constant)const H = "Foo" > "bar"//h = = True (untyped Boolean constant)Const J = true//J = True (untyped Boolean constant)Const k = ' W ' + 1//k = = ' x ' (untyped rune constant)const L = "HI"//L = = "Hi" (untyped string constant)const M = string (k)//M = = "X" (type String)constσ= 1-0.707i//(untyped complex constant)constδ=σ+ 2.0e-4//(untyped complex constant)constφ= iota*1i-1/1i//(untyped complex constant)

const IC = complex (0, C)//IC = = 3.75i (untyped complex constant)

Const iθ= Complex (0,θ)//iθ== 1.5i (type complex128)

Const HUGE = 1 <<

//Huge = = 1267650600228229401496703205376 (untyped integer constant)

Const four int8 = Huge >> 98//four = = 4 (type int8)

^1//untyped integer constant, equal to-2

uint8 (^1)//Illegal:same as Uint8 ( -2),-2 cannot be represented as a uint8 ^uint8 (1)//typed uint8 constant, same as 0xFF ^ uint8 (1) = Uint8 (0xFE) int8 (^1)//Same as int8 ( -2) ^int8 (1)//same as-1 ^ int8 (1) = 2



------------------------2013-04-14 Supplement----------------------------------------

Func Main () {

V1: =8 v1 = 8.0//compile is available, run no error FMT. Println (v1)} does not compile in C #, and the go language is feasible, why? I asked the question to everyone in the Golang-chang, thank you for helping my people. Https://groups.google.com/forum/?fromgroups=#!topic/golang-china/k1UOr_1K_ywOrganize the results for everyone

1. In the case of go without loss of precision will be 8.0 such floating-point number as an integer 8
2. The constant in Go is a high-precision number, divided into several categories:  1. There is a type: UINT (8), the type is explicitly specified, and the expression does not change.  2. Untyped: Divided into untyped integers and untyped floating-point two classes. These two classes are converted to the actual type according to the type required by the context,    such as uint8 (0) + 1.0 is uint8 (1), but uint8 (0) +1.2 will be unable to convert to Uint8 due to 1.2 error.    If the context cannot be determined (such as I, j: = 1, 2.0), then the integer untyped constant is converted to int, and the floating-point number of the untyped constant is converted to float64. Specific rules See: http://tip.golang.org/ref/spec# Constant_expressions

---------------------------------2013-04-14 End------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.