Go Language Basics

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

Defining variables

Format: var variable name variable type

Go variable declaration is very strange, put the type behind the variable name, this and we commonly used C, Java and so on, no way, get used to. go for declared but unused variables will be in the compilation phase error , strict requirements, but it should be supported.
Declaration and assignment of a single variable

varint//变量的声明123//变量的赋值varint123//变量的声明,同时赋值var321//变量的声明,同时赋值,省略变量类型,由编译器推断,这里认为是intvar"中国"//这里编译器推断是字符串string类型456//变量声明与赋值的最简方法,这种方法只能用在函数内部

The above is a commonly used variable declaration and assignment method, generally used should be var variable name = assignment.
It is important to note that global variables are generally defined by var , and function local variables are defined by : = mode.
Declaration and assignment of multiple variables

var  A, B, c int  //variable declaration  a = 1  // Assignment of a variable  b = 2  c = 3  var  A, B, C int  = 1 , 2 , 3  // Variable declaration, at the same time assignment is not feel very awkward, do not understand why go to so define him  var  A, b, C = 1< /span>, 2 , 3  // This method is generally used to define  _, B: = 2 , 3  //_ (underscore) is a special variable name, and any value given to it will be discarded. Alas, since it will be discarded, what do you do with it? 

Constant

With the const keyword in front of the variable, the variable becomes a constant
Defining variable Groups

In the go language, when you declare multiple variables, constants, or import multiple packages, you can declare them in a grouped way. This is good, better than other languages. For example, when importing multiple packages:

import"fmt"import"os"//可以用分组写成如下形式:import(     "fmt"     "os"                                                                                                                          )

Declaring multiple variables:

var a int
var B = "123"

var
a int
b = "123"
)

Declare multiple constants:

const3.1415const"Go"const(      3.1415      "Go")

Common base Types

Boolean

The value of a Boolean (bool) type can only be true or false in the Go language

Numeric type

The data range for int and uint in Go is determined by whether the platform is 32-bit or 64-bit. Go also has a directly defined number of bit types Rune, int8, Int16, Int32, Int64 and Byte, Uint8, UInt16, UInt32, UInt64. Where Rune is Int32 's nickname, Byte is Uint8 's nickname.
Note: These types do not allow mutual assignment or manipulation, or the compiler will give an error. Like what:

varint84varint32  = a

Error when compiling: (type int8) as type int32 in assignment. Also, although the length of int is 32bit, int and int32 are not interoperable.
Plural

The go language supports complex numbers, and its default type is complex128 (64-bit real number + 64-bit imaginary number). If smaller, there are also complex64 (32-bit real number + 32-bit imaginary numbers). The plural form is re + IMi, where re is the real part, IM is the imaginary part, and the last I is the imaginary unit.

varcomplex64 = 5+5ifmt.Printf(c)

Output: (5+5i)
I can't figure out what this plural can do, it's crazy, it's crazy.

String

The strings in Go are encoded using the UTF-8 character set. The string is defined with a pair of "" or an inverted quotation mark , and the type is string. In the go language, strings are immutable. What if you really want to change it?

"hello"c := []byte// 将字符串 s 转换为 []byte 类型c[0'c'string// 再转换回 string 类型fmt.Printf("%s\n", s2)

You can use + to connect two strings in go, which is like Java.
What about declaring multiple lines of string? To declare by means of an anti-quote , such as:

var s = `hello world`

Output:
Hello
World

Iota Enumeration

Keyword Iota is used when declaring enumerations, the default start value is 0, each call is added 1, it can only be used in constant expressions, and every encounter Const,iota resets to 0. See a few examples:

constiota// a=0const (  iota     //b=0  c            //c=1)//特别注意这种情况,虽然,两个iota中间隔着j=3.14,但是iota依然会加1下去,所以k的值为2const (    iota    j = 3.14    iota    

When you encounter _ (underline), you can skip without it, but the value will still add 1, for example:

typeintconst (    iota// 0    OutMono                    // 1    OutStereo                  // 2    _    _    OutSurround                // 5)

Some rules of Go programming

    • The variable that starts with the capital letter is exportable, which is the common variable that the other package can read, and the lowercase letter begins with a non-exportable, private variable.
    • A function that starts with a capital letter is the same as a public function with a common keyword in class, and a private function that starts with a secret keyword.

These rules are not customary, but they can only be accepted.

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.