Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Go language basic syntax


May 11, 2021 Go


Table of contents


Go language basic syntax

In the previous section we learned about the basic composition of the Go language, and in this section we will learn the basic syntax of the Go language.


Go tag

Go programs can consist of multiple tags, which can be keywords, identifiers, constants, strings, symbols. For example, the following GO statement consists of six tags:

fmt.Println("Hello, World!")

The 6 tags are (one per line):

1. fmt
2. .
3. Println
4. (
5. "Hello, World!"
6. )

Line separator

In a Go program, a line represents the end of a statement. E ach statement does not need to be a sign like any other language in the C family; end, as this work will be done automatically by the Go compiler.

If you plan to write multiple statements on the same line, they must use ; Artificial distinction, but we do not encourage this practice in actual development.

Here are two statements:

fmt.Println("Hello, World!")
fmt.Println("w3cschoolW3Cschool教程:w3cschool.cn")

Comments

Comments are not compiled, and each package should have comments.

Single-line comments are the most common form of comment, and you can use a single-line comment that starts with // anywhere. M ulti-line comments, also known as block comments, all start with /? and end with ./ . Such as:

// 单行注释
/*
 Author by w3cschoolW3Cschool教程
 我是多行注释
 */

Identifier

Identifiers are used to name variables, types, and other program entities. An identifier is actually a sequence of one or more letters (A-Z and a-z) numbers (0-9), underscores, but the first character must be a letter or underscore and not a number.

Here are the valid identifiers:

mahesh   kumar   abc   move_name   a_123
myname50   _temp   j   a23b9   retVal

Here are the invalid identifiers:

  • 1ab (starting with a number)
  • case (keywords for Go language)
  • a-b (operator is not allowed)

String connection

Strings in the Go language can be implemented by:

package main
import "fmt"
func main(){
    fmt.Println("Google"+"Runoob")
}

The output of the above examples is:

GoogleRunoob

Keywords

Here are 25 keywords or reserved words that will be used in 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 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

Programs typically consist of keywords, constants, variables, operators, types, and functions.

These separators may be used in the program: parenthesis (), parenthesis , and braces .

These punctuation marks may be used in the program: .、,、; ,: and ...


Spaces in the Go language

Declarations of variables in the Go language must be separated by spaces, such as:

var age int;

Proper use of spaces in statements makes the program easy to read.

No spaces:

fruit=apples+oranges;

Adding spaces between variables and operators make the program look more beautiful, such as:

fruit = apples + oranges;