Let's go together (4)

Source: Internet
Author: User
Preface

I haven't updated it for a long time, because I have been busy with other things recently. I found that after work, it was too time to go to school. Even so, I still don't forget this series.ArticleThe update process is slow, but persistence is important. ~> _ <~ +

Today, I entered the fourth article on go language learning. What should I learn today? As you said last time, yes, that's right. I learned the variable today. If you have learned the basics of other languages, it will certainly be difficult for you today. If you have not learned it, don't worry. It is actually very simple.

I. How to declare variables in the go Language

First, we create a new go language file named variable. go, here you may ask why this name is called. If you do not know this word, you can check it immediately. I believe you will understand what it means. After creating a file, use your favorite text editor or IDE to start practicing with me.

If you still remember the previous articles, I believe you have written the basics of the Go language.ProgramStructure. If you do not remember, review it. Or if you are too lazy to go back and read it again, you can continue reading it. However, it is recommended that you review the previous articles. This series of articles is very short and small. Although it is not tough, it still has the only benefit, check whether it is faster.

Basic Structure of quick memory:

1 PackageMain2 Import"FMT"3 Func main (){45FMT. Print ("Hello go")6 7}

The first example of declaring a variable:

The above example declares an int type variable A. Maybe you will ask why? In fact, the variable is the amount to change. How to change? You can try adding a row to the end of row 5th, just like writing it like this

A = 200

Then run the command to check whether the result has changed? What? How can't you run it? No way. Let's see the first article ......

If you see this, I believe you understand what is going on with the variable. You can think of the variable as a basket. As for what you put in it, for the first time, you put 100 apples in it, you can change it to 200. Of course, in the computer, it is not a basket. Hey hey, the Wiki explanation is as follows: In computer programming,VariableIs a storage location and an associated symbolic name (Identifier) Which contains some known or unknown quantity or information.

So if I don't want to put a number like 100,200, what should I do if I want a character string "Hello Go" as before?

Example 2:

Is it similar? The above declares a variable named B and of the string type (string. Then, the "I am go" value of row 6th is assigned to variable B, and B is printed. The final result is the content assigned to it.

Here we can sum up the Declaration variables in the go language in three steps.

First, what does Var = mean? Is the abbreviation of a word variable.

Then the variable name (you can call it whatever you like, but in actual programming, try to get a meaningful name, for example, age indicates age)

The final variable type (is it a string variable or an integer variable ?)

Let's look at the three steps above and what the image looks like -- language translation is [declaring a variable of the int type]

However, you will find it strange. Why isn't this reading a variable of the int type declared smoother ??? My explanation is that the English syntax is not often reversed.

Since then, you have learned how to declare variables. For other types such as the float32 type, you only need to replace the float32 type. Is it easy?

2. Declare and initialize Variables

In actual programming, in addition to declaring variables and assigning values to them, declarations and initialization are often encountered, so that two steps are not required. You only need to do this as follows:

It's much simpler. It can be done in one line. For details, refer to the following:

What is the situation? Isn't it in three steps? Why? Don't worry, it's actually the go language compiler that helped you deduce the type of the variable C, so you don't have to declare it again. Look, isn't it friendly? Write laterCodeYou can also be lazy. How did I do this? I don't know. I don't need to worry about it for the moment. I just need to remember that it can be abbreviated as this. It is not too late to look at the study further.

It was very late to write and write again, but as always, keep the short style. Although there is no content, it's over. I'll talk about it again next time, hahaha. Learning is to figure out how to be happy. If you have too many things at a time, it will be very tiring to accept something as slow as me. Finally, for today's Christmas Day, we wish readers of this article a Merry Christmas!

 

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.