Preliminary study on Golang sequential programming concept

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

Golang Study notes for easy follow-up review.

Variable

Variables are the most basic constituent elements in almost all programming languages. Essentially, a variable is a name for a piece of data storage space, and the program can request a chunk of data storage by defining a variable, and then use that storage space by referencing the variable name.
The use of variables in the go language is similar to that of the C language, but with greater flexibility.

Variable declaration

The variable declaration of the go language differs significantly from the C and C + + languages. For a purely variable declaration, the Go language introduces the keyword VAR, and the type information is placed after the variable name, as shown in the following example:
var v1 int
var v2 stringvar v3 [ten] int//array var v4 [] Int//array slice var v5 struct {
F int}
var V6 *int//Hands
var v7 map[string]intvar V8 func (a int) int//Map,key to String type, value int type
A variable declaration statement does not need to use a semicolon as a terminator. In contrast to the C language, the go language rejects the habit of having to use semicolons as a statement end tag.

Variable initialization


The var keyword can be preserved for scenarios that require initialization when declaring variables, but are no longer necessary elements, as follows:
var v1 int = 10//correct mode of use 1
var v2 = 10//correct mode of use 2, the compiler can automatically deduce the type of V2
V3: = 10//correct mode of use 3, the compiler can automatically deduce the type of V3
The effect of the above three usages is exactly the same. The third use, compared to the first, requires a much lower number of characters to be entered, and is the best choice for lazy programmers and smart programmers. The Go language also introduces another symbol that is not in C and C + + (a combination of colons and equal signs: =), which is used to explicitly express the work of declaring and initializing variables at the same time.
The specified type is no longer required, and the go compiler can deduce from the right value of the initialization expression that the variable should be declared as 4
Which type, which makes the go language look a bit like a dynamic type language, although the go language is actually a no-compromise strongly typed language (statically typed language).
Of course, the variable that appears on the left side should not have been declared, or it will cause a compilation error, such as the following:
var i inti: = 2
Results in a compilation error similar to the following:
No new variables on left side of: =


Variable assignment in Go syntax, variable initialization and variable assignment are two different concepts. The following is the assignment after declaring a variable
Process:
var v10 intv10 = 123
The variable assignment of the Go language is consistent with most languages, but the Go language provides the multi-assignment capabilities that C + + programmers expect for years, such as the following statements that Exchange I and J variables:
I, j = j, I
In languages that do not support multiple assignments, the content of the interactive two variables requires the introduction of an intermediate variable:
t = i; i = j; j = t;

Anonymous variables

When we are programming with traditional strongly typed languages, this is often the case when a function is called in order to get a value, but because the function returns multiple values, it has to define a bunch of useless variables. In go, this can be done by combining multiple returns and anonymous variables to avoid this ugly notation and make the code look more elegant.
_, _, Nickname: = "may", "Chan", "Chibi Maruko"

Constant

In the go language, constants refer to values that are known and immutable during compilation. Constants can be numeric types (including integer, float, and complex types), Boolean types, string types, and so on.
Literal constants
The so-called Literal constants (literal) refer to hard-coded constants in the program, such as:
-12
3.14159265358979323846//constant of floating-point type 3.2+12i//constant of plural type true//Boolean type constant "foo"//String constant


The literal constants of the go language are closer to the constant concept in our natural language, which is untyped. As long as the constant is within the range of the corresponding type, it can be used as a constant of that type, such as the above constant 12, which can be assigned to variables of type int, uint, Int32, Int64, float32, float64, complex64, complex128, and so on.

Constant definition

With the const keyword, you can specify a friendly name for literal constants:
Const Pi float64 = 3.14159265358979323846
Const ZERO = 0.0
Const U, v float32 = 0, 3
Const A, b, C = 3, 4, "foo"//a=3,b=4,c= "foo", untyped integer and string constant

The constant definition of go can qualify a constant type, but is not required. If you define a constant without specifying a type, it is, like a literal constant, an untyped constant. The right-hand value of a constant definition can also be a constant expression that is evaluated at compile time, such as:
Const MASK = 1 << 3 Because the assignment of a constant is a compile-time behavior, an rvalue cannot have any expression that requires a run time to produce a result, such as attempting to define a constant in such a way as to cause a compilation error:
Const HOME = os. GETENV ("HOME")
The reason is simple, os. GETENV () The return result is known only at run time and cannot be determined at compile time, so the right value cannot be defined as a constant.

Pre-defined constants

Go The language pre-defines these constants: true , false and the Iota . Iota is special and can be thought of as a constant that can be modified by the compiler, defined by constants when each const keyword appears .

With the const keyword, you can specify a friendly name for literal constants:

Const Pi float64 = 3.14159265358979323846//No type floating-point constant//untyped integer constant
Const U, v float32 = 0, 3
Const A, b, C = 3, 4, "foo"//a=3,b=4,c= "foo", untyped integer and string constant
Const ZERO = 0.0

The constant definition of go can qualify a constant type, but is not required. If you define a constant without specifying a type, it is, like a literal constant, an untyped constant. The right-hand value of a constant definition can also be a constant expression that is evaluated at compile time, such as
Const MASK = 1 << 3

Because the assignment of a constant is a compile-time behavior, an rvalue cannot have any expression that requires a run time to produce a result, such as attempting to define a constant in such a way as to cause a compilation error:
Const HOME = os. GETENV ("HOME")

The reason is simple, os. GETENV () The returned result is only known at run time, is not determined at compile time, is reset to 0, and then the number represented by the next const appears to be increased by 1, each time it appears iota. The following example can be used to understand the usage of iota basically:
The following example can be used to understand the usage of iota basically:
Const (
C0 = Iota//iota is reset to 0//C0 = = 0
C1 = IOTA//C1 = = 1
C2 = Iota//C2 = = 2

Const (
A = 1 << iota//A = = 1 (iota is reset to 0 at the beginning of each const)
b = 1 << iota//b = = 2
c = 1 << iota//c = = 4
)

Type


Go The following basic types are built into the language:

    • Previous Article Learning materials for Golang language
Top
1
Step
0
Topic recommendation
Golang Programming data Storage programming language natural language
Guess you're looking for
View Comments* The above user statements represent only their personal views and do not represent the views or positions of the CSDN website.

Core Technology Category

All Themes Hadoopaws mobile games javaandroidiosswift Smart hardware dockeropenstackvpnsparkerpie10eclipsecrmjavascript database UBUNTUNFCWAPJQUERYBIHTML5SPR Ingapache.netapihtmlsdkiisfedoraxmllbsunitysplashtopumlcomponentswindows Mobilerailsqemukdecassandracloudstackftccoremailophone couchbase Cloud computing Ios6rackspace Web Appspringsidemaemocompuware Big Data Aptechperltornadorubyhibernatethinkphphbasepuresolrangularcloud Foundryredisscaladjangobootstrap
      Personal Information

      q454684431
      • Visit:68,596 times
      • Points:1221
      • Rating: points: 1221
      • Rank: 17,757th Place
      • Original:44 articles
      • Reprinted:68 articles
      • Translation:0 Articles
      • Comments:8
      Article Search
      article Categories
    • Cloud computing ($)
    • Linux(All)
    • Network (5)
    • Git(0)
    • Python(+)
    • Life Inspiration (4)
    • Virtualization Technology (+)
    • Mirrors (2)
    • Database (3)
    • Messaging Mechanisms (4)
    • C + +(3)
    • Game 3D(0)
    • Boost Library House (5)
    • Mac(1)
    • MySQL(1)
    • Docker(in)
      Article archive
    • January 2015 (+)
    • September 2014 (1)
    • June 2014 (4)
    • May 2014 (3)
    • April 2014 (8)
    • January 2014 (2)
    • November 2013 (1)
    • September 2013 (+)
    • August 2013 (+)
      Read the rankings
    • Python eventlet concurrency Principle Analysis (2771)
    • OpenStack Working principle Analysis (2191)
    • Glance upload mirroring Process (1838)
    • Keystone installation, database configuration (1677)
    • Introduction to OpenStack Keystone (1612)
    • Python Lightweight orm---PeeWee(1404)
    • OpenStack Identity API v3 (Keystone)(1341)
    • OpenStack virtual great god Libvirt Library (1273)
    • Keystone Command Brief description (1268)
    • Getting Started with Qpid (1260)
      Comment Ranking
    • Research and implementation of the Python thread pool (2)
    • Python in the usage of with and Contextlib (2)
    • Python eventlet concurrency Principle Analysis (1)
    • OpenStack virtual great god Libvirt library (1)
    • Message mechanism Qpid Learning (Next)(1)
    • Glance upload Image Flow (1)
    • Virtualization of Qemu and KVM(0)
    • Preliminary research on Intel VT Technology performance (0)
    • Setsockopt/getsockopt Example(0)
    • Windows Socket Instance (0)
      Featured Articles
        Latest Comments
      • Analysis of Python eventlet concurrency principle

        wangzy208: Where did you turn from, can you give me the original address?

      • Glance uploading the mirroring process

        Rollingwayne: Why are the figures hanging?

      • The virtual great God Libvirt Library in OpenStack

        Lenchio: No pictures.

      • Message mechanism Qpid Learning (bottom)

        zjm713: I am learning qpid, thank you for sharing, have a question to consult, used in the embedded, but in a single process, this scene qpi ...

      • Research and implementation of Python thread pool

        q454684431: Did you get a hint that it was due to timeout error or any other error message? Or does a thread become a "runaway" state?

      • Research and implementation of Python thread pool

        Leelour: Your program In addition to the test code and ThreadPool different, there are other improvements ... Recently in the use of threadpool ...

      • Python's usage with and contextlib

        q454684431: Haha, the last line, Pro ~ ~ ~ Python's various packages are really powerful, but also a lot of ..... Not good!

      • Python's usage with and contextlib

        purple2727: How not to see the reproduced place, the article is very good, I am also learning python.

    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.