DEV Community

Cover image for Go basic syntax
toebes618
toebes618

Posted on

Go basic syntax

You will learn about the basic grammar of the Go programming language (sometimes named Golang).

The syntax of Go is similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency

Go statements

Go program statements may contain keywords, identifiers, constants, a string, a symbol etc. An example Go statement:

    fmt.Println ( "Hello, World!")

If we split it, you'd see

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

The package "fmt" (1), the Print function (3), the parameter (5)
run the "Hello world" app

You can run the hello world app using the Go compiler.

line separator

In the Go program, a statement ends automatically. Each statement is not like the other C/C++ family of languages with a semicolon ; at the end, because this is done automatically by the Go compiler.

If you intend to write multiple statements on the same line, they must be used; but in the actual development, we do not encourage this practice.

The following are two statements:

    fmt.Println ( "Hello, World!")
    fmt.Println ( "dev.to")

comments

Comments are not compiled, each line of code should have a relevant comment.

Single-line comments are the most common form of comments, you can use single-line comments begin with // anywhere.

Multi-line comments, also called block comments at the beginning have been to /* and end with */. Such as:

    // a single line comment
    / *
     Author toebes
     I am a multi-line comments
     * /

Identifier

An Identifier is used to name variables, like the type of program entities.

An identifier is actually a letter or a plurality of (A ~ Z and a ~ z) numbers (0 to 9), _ underlined sequence consisting of, but the first character must be a letter.

The following are valid identifiers:

  • move_name
  • a_123
  • myname50
  • _temp
  • j
  • a23b9
  • retVal

The following are invalid identifiers:

  • 1ab (start with a number)
  • case (keyword Go language)
  • A + b (the operator is not allowed)

keyword

Go has 25 keywords or reserved words, that you cannot use for variable names:

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 above description of these keywords, Go language there are 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

Program typically consists of the keyword, constants, variables, operators, type, and functions.

Delimiters can be used: parentheses (), brackets [] and braces {}.

The program may use these punctuation marks: ,;: and .

Go Language spaces

Go declaration of variables in the language must be separated by a space, such as:

    var age int;

Statement to make appropriate use of the space program look easy to read.

No spaces:

    fruit=apples+oranges;

Include spaces between variables and operators, the program looks more beautiful, such as:

    fruit = apples + oranges;

Top comments (0)