This is the sixth day of my participation in Gwen Challenge

These reviews

The previous article mainly introduced the basic concepts and related usage of Pointers in Go language. Go language limits the offset and operation ability of pointer types, making pointer types have the characteristics of efficient access, but pointer offset will not occur, avoiding the problem of illegal modification of sensitive data. This article will introduce you to the basic use of constants and aliases, as well as a supplementary introduction to the Go compilation tool that was missed in the previous article.

Constants and Aliases

In contrast to the variable’s runtime nature, the value of a constant is not allowed to change after it is declared. Constants can be declared with the const keyword. Constants are declared in much the same way as variables, as shown in the following example:

const str string = "Go is Good!"
Copy the code

Golang can also be used to derive the omission of the type of the constant declaration and to declare multiple constants at the same time, as in the following example:

const name = "Go is Good!"
const (
	surname = "The king"
	personalName = "Small 2"
)
Copy the code

Golang also provides syntactic features for type aliases. A type alias is another name for a type that is essentially the same type as the original type. It is equivalent to an alias for the original type. Define a type alias style as follows:

type name = T
Copy the code

In contrast, a type definition looks like this:

type name T
Copy the code

A type definition creates a new type that has the properties of the original type. We understand the difference between a type alias and a type definition using the following example:

package main

import "fmt"

type aliasInt = int // Define a type alias
type myInt int // Define a new type

func main(a)  {

	var alias aliasInt
	fmt.Printf("alias value is %v, type is %T\n", alias, alias)

	var myint myInt
	fmt.Printf("myint value is %v, type is %T\n", myint, myint)
}
Copy the code

The output is:

alias value is 0, type is int
myint value is 0, type is main.myInt
Copy the code

Int aliasInt is of the same type as aliasInt. MyInt is of the new type, but it has the same value as aliasInt.

Compile tools

To get our program up and running, we need to master some simple Golang compilation tools and commands.

go run

The go run command directly compiles and executes the main function in the source code, but does not leave any executables behind (executables are executed in temporary files that are then automatically deleted). Parameters can be added after the go run command, and these parameters are provided to the program as command line input acceptable to the code. We will demonstrate this syntax in the following sections.

Go to the directory of our hellogo.go file and execute the following command:

go run HelloGo.go
Copy the code

We can happily launch our chatbot and chat with it.

AI: I'll be fine if you play with me. It's a beautiful day. AI: It is. Didn't the sunCopy the code

go build

Concurrent compilation of code at a function level through Golang’s concurrency feature is very fast. The go build command compiles the source code into an executable, and all source code in that directory is compiled by default. You can also add multiple file names after the command. The go build command compiles the source code and outputs the executable file.

Also go to our hellogo. go file directory and execute the following command:

go build -o HelloGo
Copy the code

or

go build HelloGo.go
Copy the code

Both will generate a HelloGo executable in the current directory, which can be run directly or chatted with our chatbot.

summary

This article introduces constants and aliases in Go. Constants are not allowed to change after they are declared. Constants can be declared with the const keyword in much the same way as variables. In addition, it also introduces the related commands of the Go compilation tool.

In the next article, we’ll continue with the basic syntax of branching and looping, which are basic features of many languages.