Go language programming design learning Day1: helloworld variables and constants

Simple understanding of go language

To briefly understand the history of the Go language, in the 1980s, the original members of the Plan 9 project, including Ken Thompson, joined Google at Google, and they created the Go language. As early as September 2007, the Go language was still an
experimental project with 20% of the free time of these experts. Fortunately, by May 2008, Google discovered the huge potential of the Go language and began to fully support
the project, allowing these people to devote themselves to the design and development of the Go language. The first version of the Go language
was officially released in November 2009, and it was rapidly iterated and developed rapidly in the following two years. The first official version of the Go language was
officially released on March 28, 2012, bringing the Go language to its first eye-catching milestone.

Google's consistent style embraces open source, so we can also download the source code of go on github to view it, and even contribute some new content to go.

The following is a list of the main features of the Go language:
 Automatic garbage collection
 Richer built-in types
 Multiple return values ​​​​for functions
 Error handling
 Anonymous functions and closures
 Types and interfaces
 Concurrent programming
 Reflection
 Language interactivity

Configure development environment

  • Download go

  • Set gopath (go code storage space), goroot (go language sdk/bin folder)

  • vscode imports multiple plugins from go

    Insert image description here

  • Create go code storage structure

image-20221215160913042

The first go program

//  声明文件所存在的位置 packge
package main

// 进入需要使用的工具库(包)
import "fmt"

// main 函数 程序的入口
func main(){
    
    
	fmt.Println("hello golang")
}

Commonly used commands

  • Execute go file go run xxxx
  • Generate executable file go build xxx

variable

​ We can think of a program as a small world. No matter which world is part of it, variables are part of the program world.

A variable is actually a representation of a data space stored in memory. It may have different types.

image-20221215161403147

The variable declaration method of Go language is obviously different from that of C and C++ languages. For pure variable declaration, Go language introduced

The variable declaration method of Go language is obviously different from that of C and C++ languages. For pure variable declaration, Go language introduces
the keyword var, and the type information is placed after the variable name. The example is as follows:

var v1 int
var v2 string
// 数组
var v3 [10]int
// 数组切片
var v4 []int
var v5 struct {
    
    
  f int
  }
 // 指针
var v6 *int
 // map,key为string类型,value为int类型
var v7 map[string]int
var v8 func(a int) int

Variable declaration statements do not need to be terminated with a semicolon. Compared with C language, Go language abandons
the habit of using a semicolon as the end mark of a statement.

The keyword var, and the type information is placed after the variable name. The example is as follows:

  1. var v1 int
  2. var v2 string
  3. var v3[10]int
  4. // array
  5. var v4 []int
  6. // array slice
  7. var v5 struct {
  8. f int
  9. }
  10. where v6 *int
  11. // pointer
  12. var v7 map[string]int
  13. // map, key is string type, value is int type
  14. var v8 func(a int) int
  15. Variable declaration statements do not need to be terminated with a semicolon. Compared with C language, Go language abandons the requirement that statements be marked with semicolons.
  16. The convention of marking the end of a statement.

Variable usage process

Declare variables in go language (Types have default values ​​that can be used directly) must be used, otherwise a compilation exception will occur.

  • statement
  • copy
  • use

Example:

	var age int
	age = 18
	fmt.Println("age=",age)

Variable initialization method

​ For scenarios that require initialization when declaring variables, the var keyword can be retained, but it is no longer a necessary element.

var v1 int = 10 // 正确的使用方式1
var v2 = 10
// 正确的使用方式2,编译器可以自动推导出v2的类型
v3 := 10
// 正确的使用方式3,编译器可以自动推导出v3的类型

Specifying a type is no longer necessary. The Go compiler can deduce from the rvalue of the initialization expression what type the variable should be declared as
. This makes the Go language look a bit like a dynamically typed language, although the Go language is actually A truly strongly typed language
(statically typed language).

Things to note: = The variable on the left should not have been declared otherwise an exception will occur.

var i int
i := 2
会导致类似如下的编译错误:
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 process after declaring a variable
:

var v10 int
v10 = 123

Variable assignment in Go language is consistent with that of most languages, but Go language provides the multiple assignment function that C/C++ programmers have been waiting for for many years
, such as the following statement to exchange i and j variables:

i, j = j, i

In languages ​​that do not support multiple assignment, exchanging the contents of two variables requires the introduction of an intermediate variable:

t = i; i = j; j = t;

anonymous variable

In Go, this situation can be avoided by using a combination of multiple returns and anonymous variables to avoid the ugly way of defining some unnecessary variables, making the code look more elegant.

​ Assume that the GetName() function is defined as follows. It returns 3 values, namely firstName, lastName and
nickName:

func main(){
    
    
	// var v1 int  = 10
	// var v2 = 10
	// v3:= 3
	// fmt.Println(v1,v2,v3);
	_, _, nickName:= GetName()
	fmt.Println(nickName);
}
//若只想获得nickName,则函数调用语句可以用如下方式编写
func GetName()(firstName,lastName,nickName string){
    
    
	return "may", "chan", "chibi maruko"
}

Output result:

chibi maruko

​ This usage can make the code very clear and basically block out content that may confuse code readers, thereby greatly reducing the complexity of communication and the difficulty of code maintenance.

constant

In the Go language, constants refer to values ​​that are known during compilation and cannot be changed. Constants can be numeric types (including integer, floating point and complex number types), Boolean types, string types, etc.

literal constant

​ The so-called literal constants refer to hard-coded constants in the program, such as:

-12
3.14159265358979323846
// 浮点类型的常量
3.2+12i
// 复数类型的常量
true
// 布尔类型的常量
"foo"
// 字符串常量

​ It is untyped in the Go language. As long as this constant is within the value
range of the corresponding type, it can be used as a constant of that type.

constant definition

​ With the const keyword, you can give a literal constant a friendly name:

const Pi float64 = 3.14159265358979323846
const zero = 0.0 // 无类型浮点常量
const (
size int64 = 1024
eof = -1 // 无类型整型常量
)
const u, v float32 = 0, 3 // u = 0.0, v = 3.0,常量的多重赋值
const a, b, c = 3, 4, "foo"
// a = 3, b = 4, c = "foo", 无类型整型和字符串常量

​Go's constant definitions can qualify the constant type, but it is not required. If a constant is defined without specifying a type, it
is an untyped constant just like a literal constant.
The rvalue of a constant definition can also be a constant expression that is evaluated at compile time, such as

const mask = 1 << 3

Predefined constants

Go language predefines these constants: true, false and iota.

​ iota is special and can be considered a constant that can be modified by the compiler. It is reset to 0 every time the const keyword appears. Then before the next
const appears, every time iota appears, the number it represents will Automatically increases by 1.

You can basically understand the usage of iota from the following examples:

const (
// iota被重设为0
c0 = iota// c0 == 0
c1 = iota// c1 == 1
c2 = iota// c2 == 2
)
const (
a = 1 << iota// a == 1 (iota在每个const开头被重设为0)
b = 1 << iota// b == 2
c = 1 << iota// c == 4
)
const (
u = iota * 42 // u == 0
v float64 = iota * 42 // v == 42.0
w = iota * 42// w == 84
)
const x = iota// x == 0 (因为iota又被重设为0了)
const y = iota// y == 0 (同上)
    

​ If the expressions of two const assignment statements are the same, then the latter assignment expression can be omitted. Therefore, the first two const statements above can be shortened to:

const (
// iota被重设为0
c0 = iota // c0 == 0
c1 // c1 == 1
c2 // c2 == 2
)
const (
a = 1 <<iota // a == 1 (iota在每个const开头被重设为0)
b // b == 2
c // c == 4
)

enumerate

​ An enumeration refers to a series of related constants, such as the following definition of each day of the week. Through the example in the previous section, we saw that a set of constants can be defined using const followed by a pair of parentheses. This definition method is usually used to define enumeration values ​​in the Go language. The Go language does not support many of the enum keywords that other languages ​​explicitly support.

const (
Sunday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
numberOfDays // 这个常量没有导出
)

​ Like other symbols in the Go language, constants starting with an uppercase letter are visible outside the package.
In the above example, numberOfDays is private within the package, and other symbols can be accessed by other packages.

Guess you like

Origin blog.csdn.net/doomwatcher/article/details/128333701