laitimes

The basic syntax of the Go language differs from other languages

author:Lingxi meets you

Go language as a programming language that appeared relatively late, in its native support for high concurrency, cloud native and other areas of excellent performance, like the current more popular container orchestration technology Kubernetes, container technology Docker are written in the Go language, like Java and other object-oriented languages, although it can also do cloud-native related development, but the degree of support is far less than the Go language, with its language features and simple programming methods, to make up for the shortcomings of other programming languages to a certain extent, It once became a popular programming language.

Recently in learning go, I have used C#, Java and other object-oriented programming languages before, and found that there are many programming styles that are different from other languages, and it is not as good as bad writing, to sum up, and compare with other languages. Only the differences are summarized here, and the specific syntax is not introduced in detail.

The best time to plant a tree is ten years ago, followed by now.

Basic syntax

1. Variables

1) Go variable declaration statements do not require the use of semicolons as separators

var v1 int
var v2 string           

2) Several variables that need to be declared can be put together

var(
  v1 int
  v2 string
)           

3) When the variable is initialized, you can add an equal sign directly after the variable like other languages, and the value to be initialized after the equal sign, or you can use the variable name: = the simple way of the variable value

var v1 int=10//正常的使用方法
var v2=10//编译器可以推导出v2的类型
v3:=10//简单写法 明确表达同时进行变量声明和初始化工作           

3) Variable assignment The go language's variable assignment is the same as in most languages, but the Go language provides multiple assignment functions, such as the following statement that exchanges i and j variables:

i,j=j,i           

In languages that do not support multiple assignments, exchanging the values of two variables requires the introduction of an intermediate variable:

t=i;i=j;j=t;           

4) Anonymous variables

When using other languages, sometimes to get a value, but because the function returns multiple values and have to define a lot of variables that do not have, Go can use multiple return values and anonymous variables to avoid this writing, so that the code looks more elegant.

Suppose the GetName() function returns 3 values, namely firstName, lastName, and nickName

func GetName()(firstName,lastName,nickName string){
  return "li","mingqi","dove"
}           

If the pointer gets nickName, the function call can be written like this

_,_,nickName:=GetName()           

This writing method makes the code clearer, which greatly reduces the complexity of communication and the difficulty of maintenance.

2. Constants

1) Basic constants

Constants are defined using the keyword const, which can qualify constant types, but are not required, and are untyped constants if no type of constant is defined

2) Predefined constants

The Go language predefines these constants true, false, and iota

iota is special and can be tasked with being a constant that can be modified by the compiler, reset to 0 when each const keyword appears, and then for each iota that appears before the next const appears, the number it represents is automatically added to 1.

//预定义常量:true,false和iota
  // iota可以被认为是一个编译器修改的常量,在没有个const关键字出现时被重置为0,然后在下一个const出现之前,
  //没出现一次iota,其所在的数字会自动增1
  const (
    c0 = iota//c0=0
    c1 = iota//c1=1
    c2 = iota//c2=2
  )
  fmt.Println("c0:",c0)
  fmt.Println("c1:",c1)
  fmt.Println("c2",c2)
  const (
    x = 1 << iota // x==1(iota在每个const开头被重设为0)
    y = 1 << iota// y==2
    z = 1 << iota// z==4

  )
  fmt.Println("x:",x)
  fmt.Println("y:",y)
  fmt.Println("z:",z)
  const (
    r = iota*42
    t float32 =iota*42
    w =iota*42
  )
  fmt.Println("r:",r)
  fmt.Println("t:",t)
  fmt.Println("w:",w)
  const aa = iota
  const (
    bb = iota
  )
  fmt.Println("aa:",aa)
  fmt.Println("bb:",bb)
  const mark =2 << 3 //编译期运算的常量表达式
  fmt.Println("mark:",mark)           

3) Enumeration

The Go language does not support the emum keyword, but uses methods declared by constants to define enumeration values:

const(
  Sunday=iota
  Monday
  Tuesday
  Wendnesday
  Thursday
  Friday
  Saturday
  numberOfDays
)           

3. Type

1) Int and int32 are considered two different types in the Go language

2) The Go language defines two floating-point float32 and float64, of which the former is equivalent to the float type of C and the latter is equivalent to the double type of C

3) Go language supports complex types

Complex numbers actually consist of two real numbers (represented by floating-point numbers in a computer), one representing real and one representing imaginary. That's the mathematical complex number

Representation of a complex number

var value complex64//由两个float32 构成的复数
value1=3.2+12i
value2:=3.2+12i //value2是complex128类型
value3:=complex(3.2,12)//结果同value2           

Real and imaginary parts

For a complex number z = complex(x,y), the real part of the complex number, that is, x, can be obtained through the Go built-in function real(z), and the imaginary part of the complex number is obtained by imag(z), that is, y

4) Array (value type, length cannot be modified again after definition, each pass will produce a copy. )

The array can be declared in the [n]type mode or the make keyword

[10] int
a:=make([10] int)           

5) Array slice

Array slices make up for the lack of arrays, and their data structures can be abstracted into the following three variables:

  • A pointer to a native array
  • The number of elements in the array slice
  • The array slices the allocated storage space.

6) Map In the go language, Map does not need to introduce any libraries, which is very convenient to use

4. Conditional statement If

  1. Conditional statements do not require parentheses to enclose conditions
  2. Regardless of how many statements there are in the body of the statement, the curly brace {} must be present
  3. The left curly brace {must be on the same line as if or else.<
  4. Before the conditional statement after if, you can add a variable initialization statement, using a semicolon interval
  5. In functions with return values, it is not allowed to include the final return statement in if... else structure, otherwise the compilation fails.

5. Select statement (switch)

  1. The left curly brace {must be on the same line as the switch.}
  2. Conditional expressions do not restrict constants or certificates
  3. Multiple result options can appear in a single case
  4. There is no need for a break in Go to explicitly exit a case
  5. Only if you explicitly add the fallthrough keyword to the case will you proceed to the next case
  6. It is possible not to set the expression after the switch, in which case the entire switch structure and if... The logic of else acts the same

6. Looping statement for

The Go loop statement only supports the for keyword, not while and do-while

  1. The left {curly braces must be on the same line as for
  2. The Go language does not support multiple assignment statements in comma intervals, and multiple variables must be initialized using parallel assignment statements
  3. Go's for loop also supports continuity and break to control loops, but it provides a higher-level break that lets you choose which loop to break
for j:=0;j<5;j++{
 for i:=0;i<10 i++{
 if i>5{
 break JLoop //break语句终止的是JLoop标签处的外层循环
 }
 fmt.Println(i)
 }
}
JLoop:
// ...           

7. Jump statement

The semantics of the goto statement are very simple, that is, to jump to a tag within this function

fun myfunc(){
  i:=0
  HERE:
  fmt.Println(i)
  i++
  if i<10{
    goto HERE
  }
}           

I will introduce it here today, and I will summarize the differences and uses of the Go language in other aspects such as concurrent programming, object-oriented, network programming, etc. Hope that helps.

Read on