Environment installation
The Go language supports the following systems:
-
- Linux
- Freebsd
- Mac OS X (also known as Darwin)
- Window
The installation package is: https://golang.org/dl/.
Download the corresponding version directly under Windows to install it.
Hello World
To create a new hello.go, enter the following code:
Package Mainimport "FMT" Func Main () { fmt. Println ("Hello, world!")}
Command line run go run Hello. Go
Note: The package name is not related to the owning folder, and the entry package must be main. Import into the package. Func defines the function, and the function name of the entry program must be main.
IDE
Liteide is an open-source, cross-platform, lightweight go Language integrated development environment (IDE). Project Address: Https://github.com/visualfc/liteide
JetBrains goland,https://www.jetbrains.com/go/download/developed by JetBrains
GO language Structure
The basic components of the Go language are the following sections:
-
- Package Declaration
- Introduction Package
- Function
- Variable
- Statements & Expressions
- Comments
Now let's take a look at the simple code, which outputs "Hello world!":
Package Mainimport "FMT" Func Main () {/ * This is my first simple program */ FMT. Println ("Hello, world!")}
1. The first line of code packages main defines the package name. You must indicate which package the file belongs to in the first line of the non-comment in the source file, for example: packages Main. The package main represents a program that can be executed independently, with each Go application containing a bundle called Main.
2, the next line of import "FMT" tells the Go compiler that this program needs to use the FMT package (the function, or other elements), the FMT package implements the format IO (input/output) function.
3. The next line, func main () , is the function that the program starts executing. The main function is what each executable program must contain, typically the first function executed after startup (if there is an init () function, the function is executed first).
4, the next line/*...*/is a comment, will be ignored when the program executes. Single-line comments are the most common form of annotation, and you can use a single-line comment at any point, starting with//. Multiline comments are also called block annotations, which begin with/* and end with a/* and cannot be nested, and multiline annotations are typically used for package document descriptions or comment blocks of code snippets.
5, the next line of FMT. Println (...) You can output a string to the console and automatically increment the newline character \ n at the end.
Go language base syntax line delimiter
In the Go program, a line represents the end of a statement. Semicolons (;) are optional, if you intend to write multiple statements on the same line, they must be used; split, but do not use this practice in real-world development.
Comments
Comments are not compiled, and each package should have related comments. Go comments and C,java, there are single-line comments//, and multiline comments/* ... */
Single-line Comment
/*
I'm a multi-line comment
*/
Identifier
Identifiers are used to name program entities such as variables, types, and so on. An identifier is actually a sequence of one or more letters (A~Z and A~z) numbers (0~9) and underscores _, but the first character must be a letter or an underscore and not a number.
The following are valid identifiers:
MAHESH KUMAR ABC move_name a_123myname50 _temp J a23b9 RetVal
The following are invalid identifiers:
-
- 1AB (starting with a number)
- Case (keywords for Go language)
- A+b (operator is not allowed)
Here are the 25 keywords or reserved words that will be used in the Go code:
| Break |
Default |
Func |
Interface |
Select |
| Case |
Defer |
Go |
Map |
struct |
| Chan |
Else |
Goto |
Package |
Switch |
| Const |
Fallthrough |
If |
Range |
Type |
| Continue |
For |
Import |
Return |
Var |
In addition to the keywords described above, the Go language also has 36 predefined identifiers:
| Append |
bool |
Byte |
Cap |
Close |
Complex |
Complex64 |
complex128 |
UInt16 |
| Copy |
False |
Float32 |
Float64 |
Imag |
Int |
int8 |
Int16 |
UInt32 |
| Int32 |
Int64 |
Iota |
Len |
Make |
New |
Nil |
Panic |
UInt64 |
| Print |
println |
Real |
Recover |
String |
True |
UInt |
Uint8 |
UIntPtr |
These delimiters may be used in the program: parentheses (), brackets [], and curly braces {}. Programs are typically made up of keywords, constants, variables, operators, types, and functions.
These punctuation marks may be used in the program: Dot number. comma, semicolon; Colon: three dot number ...
Go Language data type
In the Go programming language, data types are used to declare functions and variables.
1. Boolean type
The value of a Boolean can only be a constant true or false.
2. Number Type
integer int and floating-point Float,go languages Support integer and floating-point numbers, and natively support complex numbers, where bits are operated in complement.
Integral types are signed and unsigned:
Uint8,uint16,uint32,uint64,int8,int16,int32,int64 represents unsigned and signed 8-bit, 16-bit, 32-bit, 64-bit integer, respectively
Floating point type:
Float32 32-bit floating-point number, float64-bit floating-point number. Complex64 32-bit real and imaginary, complex128 64-bit real and imaginary numbers
Other number types:
byte similar to Uint8
Rune Similar to Int32
UINT 32 or 64 bits
int is the same size as uint
UIntPtr unsigned integer for holding a pointer
3. String Type:
A string is a sequence of characters concatenated with fixed-length characters. The string of Go is connected by a single byte. The byte of the Go language uses UTF-8 encoding to identify Unicode text.
4. Derived type:
Including:
(a) pointer type (Pointer)
(b) Array type
(c) structured type (struct)
(d) Channel type
(e) Function type
(f) Slice type
(g) interface type (interface)
(h) Map type
Go Language Primer (i)