Easy Tutorial
❮ Go Error Handling Go Passing Arrays To Functions ❯

Basic Syntax of Go Language

In the previous chapter, we learned about the basic structure of the Go language. In this chapter, we will study the basic syntax of the Go language.


Go Tokens

A Go program can be composed 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 Separator

In a Go program, a line represents the end of a statement. Unlike other C-family languages, each statement does not need to end with a semicolon ;, as this task is automatically handled by the Go compiler.

If you intend to write multiple statements on the same line, they must be manually separated by semicolons ;, but this practice is not encouraged in actual development.

Here are two statements:

fmt.Println("Hello, World!")
fmt.Println("tutorialpro.org: tutorialpro.org")

Comments

Comments are not compiled and every package should have relevant comments.

Single-line comments are the most common form of comments. You can use single-line comments starting with // anywhere. Multi-line comments, also known as block comments, start with /* and end with */. For example:

// Single-line comment
/*
 Author by tutorialpro.org
 This is a multi-line comment
 */

Identifiers

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), digits (0~9), or underscores _, but the first character must be a letter or underscore, not a digit.

Here are valid identifiers:

mahesh   kumar   abc   move_name   a_123
myname50   _temp   j   a23b9   retVal

Here are invalid identifiers:


String Concatenation

String concatenation in Go can be achieved using the + operator:

Example

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

The output of the above example is:

Googletutorialpro

Keywords

The following are 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 these keywords, Go 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 are generally composed of keywords, constants, variables, operators, types, and functions.

Programs may use these delimiters: parentheses (), brackets [], and braces {}.

Programs may use these punctuation marks: ., ,, ;, :, and .


Whitespace in Go

Variable declarations in Go must be separated by whitespace, for example:

var age int;

Using whitespace appropriately in statements makes the program easier to read.

Without whitespace:

fruit=apples+oranges;

With whitespace between variables and operators, the program looks more aesthetically pleasing, such as:

fruit = apples + oranges;

Formatting Strings

In Go, strings can be formatted using fmt.Sprintf or fmt.Printf:

Sprintf Example

package main

import (
    "fmt"
)

func main() {
    result := fmt.Sprintf("Name: %s, Age: %d", "John", 30)
    fmt.Println(result)
}

The output of the above example is:

Name: John, Age: 30
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)
}

Output result:

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

Output result:

Code=123&endDate=2020-12-31

For more information, see:

❮ Go Error Handling Go Passing Arrays To Functions ❯