Go Language Basic Syntax
In the previous chapter we learned about the basic structure of the Go language; in this chapter we will learn the basic syntax of Go.
Go Tokens
A Go program can consist of multiple tokens, which can be keywords, identifiers, constants, strings, or symbols. For example, the following Go statement consists of 6 tokens:
fmt.Println("Hello, World!")
The 6 tokens are (one per line):
1. fmt
2. .
3. Println
4. (
5. "Hello, World!"
6. )
Line Separators
In a Go program, a line represents the end of a statement. Each statement does not need to end with a semicolon ; as in other languages of the C family, because this work is done automatically by the Go compiler.
If you intend to write multiple statements on the same line, they must be manually separated by ;, but this practice is not encouraged in actual development.
The following are two statements:
fmt
.
Println
(
"Hello, World!"
)
fmt
.
Println
(
"Runoob Tutorial:runoob.com"
)
Notes
Comments are not compiled, and every package should have relevant comments.
Single-line comments are the most common form of comment; you can use single-line comments starting with // anywhere. Multi-line comments are also called block comments; they start with /* and end with */. For example:
// single-line comment
/*
Author by Runoob Tutorial
我是多行注释
*/
Identifiers
Identifiers are used to name program entities such as variables and types. An identifier is actually a sequence of one or more letters (A~Z and a~z), digits (0~9), and underscores _, but the first character must be a letter or underscore, not a digit.
The following are valid identifiers:
mahesh kumar abc move_name a_123
myname50 _temp j a23b9 retVal
The following are invalid identifiers:
- 1ab (starts with a digit)
- case (a keyword in Go)
- a+b (operators are not allowed)
String concatenation
String concatenation in Go can be achieved with +:
Example
package
main
import
"fmt"
func
main
()
{
fmt
.
Println
(
"Google"
"Runoob"
)
}
The output of the above example is:
GoogleRunoob
The keyword
The following lists the 25 keywords or reserved words 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 introduced above, Go 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 |
| println | real | recover | string | true | uint | uint8 | uintptr |
A program generally consists of keywords, constants, variables, operators, types, and functions.
These separators may be used in a program: parentheses (), brackets [], and braces {}.
These punctuation marks may be used in a program: ., ,, ;, :, and ….
Whitespace in Go
In Go, whitespace is generally used to separate identifiers, keywords, operators, and expressions to improve code readability.
Variable declarations in Go must be separated by spaces, for example:
var x int
const Pi float64 = 3.14159265358979323846
Using spaces between operators and operands makes a program easier to read:
No spaces:
fruit=apples+oranges;
Adding spaces between variables and operators makes the program look nicer, for example:
fruit = apples + oranges;
Use spaces between keywords and expressions.
For example:
if x > 0 {
// do something
}
In function calls, use a space between the function name and the left-hand equals sign, and also between parameters.
For example:
result := add(2, 3)
Format a string
In Go, use fmt.Sprintf or fmt.Printf to format a string and assign it to a new string:
- Sprintf Generates a formatted string according to the format parameters and returns it.
- Printf Generates a formatted string according to the format parameters and writes it to standard output.
Sprintf Example
package
main
import
(
"fmt"
)
func
main
()
{
// %d represents an integer, %s represents a string
var
stockcode
=
123
var
enddate
=
"2020-12-31"
var
url
=
"Code=%d&endDate=%s"
var
target_url
=
fmt
.
Sprintf
(
url
,
stockcode
,
enddate
)
fmt
.
Println
(
target_url
)
}
The output is:
Code=123&endDate=2020-12-31
Printf Example
package
main
import
(
"fmt"
)
func
main
()
{
// %d represents an integer, %s represents a string
var
stockcode
=
123
var
enddate
=
"2020-12-31"
var
url
=
"Code=%d&endDate=%s"
fmt
.
Printf
(
url
,
stockcode
,
enddate
)
}
The output is:
Code=123&endDate=2020-12-31
For more information, see: