Let's go (5) together)

Source: Internet
Author: User
Preface

In a twinkling of an eye, it's already in 2013. In the new year, I will continue to learn go. This series has accidentally crossed the year. In the previous article, we have a preliminary understanding of the variable declaration in the go language. Today we will continue to talk about variables. The article is updated slowly. Please forgive me because the company's projects are too busy and there is basically no time during the day. I took the time to write them at night, and my level is limited. It is inevitable that there is something wrong with it.

I. Talking about go language Variables

In the previous article, we know that variables are declared using the VaR keyword in the go language. If we declare variables, we do not assign values to them. Are there any initial values for those variables?

To prove this, It's actually easy. You just need to write some code and print it out. Test A part of the data below:

 1 package main 2 import "fmt" 3 func main(){ 4     var a int 5     var b float32 6     var c bool 7     var d string 8     var e []int 9     var f map[string] int10     var g *int11     if nil == e{12         fmt.Print("e is nil \n")13     }14     if nil == f{15         fmt.Print("f is nil \n")16     }17     fmt.Print(a,b,c,d,e,f,g)18 }

 

Through the above example, we have made it clear that the variables declared in the go language have initial values by default. It can be roughly divided into the following types:
1. Int int32 int64-> 0

2. bool-> false

3. float32 float64-> 0.0

4. pointer (pointer) map array slice...-> Nil

You can declare multiple variables at a time in the go language:

VaR a, B, c int

For different types of variables, you can:

VaR (

A int

B bool

)

Of course, you can also directly declare and initialize multiple variables:

VaR a, B, c Int = 1, 2, 3

VaR A int, C bool = 1, false

To simplify the process, you can also use the type inference feature to declare initialization as follows:

VaR a, B = false, "am"

A, B: = true, 3

The above is just an introduction to the simplest types of declaration initialization in the go language. More complex types, such as arrays, maps, functions, interfaces, etc, this type will be detailed in the future, because if we introduce it here, it will make the length of this article longer, too much content, in line with the consistent short purpose, so please be patient and so on.

Ii. Basic Types of Go Language

Type length des

  1. Bool 1 true, false. ============ Do not treat the value of limit as true.

  2. Byte 1 uint8 alias

  3. Rune 4 int32 alias. Represents a maximum Unicode Code Point. INT/uint may be 32bit or 64bit based on the platform on which the node is run.

  4. Int8/uint8 1-128 ~ 127; 0 ~ 255

  5. Int16/uint16 2-32768 ~ 32767; 0 ~ 65535int32/uint32 4-2.1 billion ~ 2.1 billion, 0 ~ 4.2 billion

  6. Int64/uint64 8

  7. Float32 4 accurate to 7 worker digits

  8. Float64 8 accurate to 15 bytes digits

  9. Complex64 8

  10. Complex128 16

  11. Uintptr success is enough to save the 32-bit or 64-bit integer of the pointer.

  12. Array value type, for example, [2] int

  13. Struct Value Type

  14. String Value Type

  15. Map reference type

  16. Channel reference type

  17. Slice cited shard type: [] int

  18. Interface connector type

  19. Function Type

 

Wow, it's a huge pile above, but it's okay. Most of them are still very familiar. If you have written other languages, as for the unfamiliar ones, they will be cracked one by one in the days to come, I will not elaborate here, because there is nothing to say, just like your name. Why do you call this name? Okay, you can start with your ancestor's last name, and then pull the name and meaning. Of course, variables can also be pulled, and memory can be pulled ...... but it doesn't make much sense now, so the basics of variables are here today. If you can't help it, go to the Internet to learn more.

Iii. Variable type conversion

After talking about various variables, you must also talk about how to convert them. Remember that in the go language, implicit conversion is not supported and you need to specify it. As shown below:

VaR A Int = 2

VaR B float64 = float64 ()

The rule is simple: <the type you want to convert> (original variable)

Is it easy? Please try other types on your own. I am too lazy and don't want to repeat them mechanically.

 

Iv. Constants

After talking about the variable, let's talk about the constant. The constant is like its name. What is the normal amount? Of course not. Haha, constants can be understood as not variables (...... *&&...... In short, constants are the amount of unchangeable values during the program running.

The following describes how to declare and define constants:

1 const x int32 = 1992 const y = "Hello"3 const a, b, c = "apple", true, "jobs"  4 const (5     male= "jack"6     fmale = false7 )

Today we are here to help you.

 

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.