WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Basic Syntax


In the previous chapter, we have already understood the basic structure of the Go language. In this chapter we will learn the basic syntax of the Go language.


Go tag

Go program can consist of multiple tags, which can be keywords, identifiers, constants, strings, symbols. The following GO statement consists of 6 tags:

fmt.Println("Hello, World!")

6 tags are (one per line):

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

Line separator

In the Go program, a line represents the end of a statement. Each statement does not need to end with a semicolon like other languages in the C family, as these tasks are done automatically by the Go compiler.

If you plan to write multiple statements on the same line, they must be used; artificially differentiated, but we don't encourage this in actual development.

The following are two statements:

fmt.Println("Hello, World!")
fmt.Println("Welookups tutorial:Welookups.com")

Comment

Comments will not be compiled, and each package should have a comment.

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

// Single line comment
/*
  Author by Welookups tutorial
  I am a multi-line comment
 */

Identifier

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) numbers (0~9) and underscores, but the first character must be a letter or an underscore and cannot be 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 (starting with a number)
  • case (the keyword for Go)
  • a+b (operators are not allowed)

String connection

The string for the Go language can be implemented with +:

Instance

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

The output of the above example is:

GoogleWelookups

Keywords

Here are the 25 keywords or reserved words that will be used in the Go code:

breakdefaultfuncinterfaceselect
casedefergomapstruct
chanelsegotopackageswitch
constfallthroughifrangetype
continueforimportreturnvar

In addition to the keywords described above, 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 generally consist of keywords, constants, variables, operators, types, and functions.

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

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


Space in Go language

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

var age int;
The appropriate use of spaces in the

statement makes the program easier to read.

No spaces:

fruit=apples+oranges;

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

fruit = apples + oranges; /span>