Go language basic syntax


Release date:2023-09-08 Update date:2023-10-13 Editor:admin View counts:262

Label:

Go language basic syntax

We already know about the last chapter. The basic structure of Go language, which we will learn in this chapter Go basic grammar of the language.

Go marker

Go program can be composed of multiple tags, which can be keywords, identifiers, constants, strings, and symbols. As follows 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 delimiter

In Go a program, one line represents the end of a statement. Each statement does not need to be like C other languages in the family use semicolons as well. ; at the end, because all of this work will be done by Go compiler completes automatically.

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

Here are two statements:

fmt.Println("Hello, World!")
fmt.Println("Novice Tutorial:runoob.com")

Annotation

Comments will not be compiled, and each package should have related comments.

Single-line comments are the most common form of comments, and you can use them anywhere with // single-line comment at the beginning. Multiline comments, also known as block comments, have been marked with /* start with a */ end. Such as:

// Single-Line Comments
/*
 Author by Novice Tutorial
 I am a multi line comment
 */

Identifier

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

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 number)

  • case ( Go keywords of language)

  • a+b (operators are not allowed)

String concatenation

The string of the Go language can be passed through the + :

Example

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

The output result of the above example is:

GoogleRunoob

Keyword

The following is listed Go 25 keywords or reserved words that will be used in the 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 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

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

These delimiters may be used in the program: parentheses () , square brackets [] and curly braces {} .

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

Spaces in Go language

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

var age int;

The proper use of spaces in the statement can make the program easier to read.

No spaces:

fruit=apples+oranges;

Add a space between variables and operators to make the program look more beautiful, such as:

fruit = apples + oranges;

Format string

Go use in language fmt.Sprintf format the string and assign a value to the new string:

Example

package main
import (
    "fmt"
)
func main() {
   // %d represents an integer number, %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 as follows:

Code=123&endDate=2020-12-31

Powered by TorCMS (https://github.com/bukun/TorCMS).