Go basic syntax
Tags: go, beginners
Published: 2020-03-01 19:22:40

You will learn about the basic grammar of the <a href="https://golang.org/">Go programming language</a> (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:

```go
    fmt.Println ( "Hello, World!")
```    

If we split it, you'd see
    
```go    
    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 <a href="https://golangr.com/hello-world/">hello world</a> 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:
    
```go
    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:

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

## Identifier

An Identifier is used to name <a href="https://golangr.com/variables/">variables</a>, 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:

```go
    var age int;
```

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

No spaces:

```go 
    fruit=apples+oranges;
```

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

```go
    fruit = apples + oranges;
```