Get the App
SLTechnology News&Howtos  ›  Servers  › 

Basic syntax of go language

Shulou Source: shulou.com Published: 2022-06-02 06:16:01 09月15日 Update

The basic syntax of go defines variables

There are two ways to define variables in the go language: var, as follows

Var a = 1

Assign the a variable to 1 in the second way ": =", as follows:

A: = 1

As above, assign a variable to 1`Note: variable name: = variable value, this method is only applicable to the first assignment of a variable, if the variable has been assigned, such as the an above has been assigned to 1, at this time, if you want to re-assign a value in the use of a: = 2, you can change the assignment directly a = 2

Define the function:

There is no class keyword or class concept in the go language. Go is mainly a function-oriented and interface-oriented programming language.

Define the keyword func of the function, as follows:

Syntax: the content in [] is optional

Func function name ([parameter name] [parameter type]) [function return value type] {function specific logic [return] [returned content]}

Loop:

In go, there are only for loops and no other loops.

The basic way of writing:

For I: = 0; I = 10; iTunes + {loop body content}

4There is nothing wrong with it. Judge

1.if judgment:

If condition {processing logic} else {processing logic} switch and case judgment

Syntax: switch [variable name] {case condition 1: processing logic case condition 2: processing logic default: processing logic} the above method is a bit similar to case in shell

5. Slice slice

Define slice:

Variable name: = [length] type, as follows: define a slice of int type with length 10

Arr: = [10] int

Define a variable length slice:

Var arr1 = [...] Int {} or: arr: = [...] int {0Pert 1je 2jue 3jre 4jre 5jr 6jr 7}

Map type:

B: = make (map [string] string) c: = make (map [string] int) above is the definition of two map, one is bmeme c make is to open up memory space, the map keyword, [] is the key type, string and int are both value types

Tags: Variable type function logic language way processing loop syntax key keyword content condition length parameter foundation language foundation two memory writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info OPPO Reno Shulou Technology vpn