Golang self-study series

Why this series?

Because I want to move closer to architecture direction ah.
About architecture, in fact, the architecture of the book I read, "clean architecture of the Road", but also "to achieve field drive design." But I feel clear enough, so I bought a framework in the relevant column geeks time, this column is written in programming languages go, in order to better study and understand, so have this series.

When I go to use vscode be programmed to always display a warning about

type Service struct {
        a *ClassName
}
exported type Service should have comment or be unexported

This is because you install plug-ins goplsto the code will be some rules. That is, if you set a variable / class, if at the beginning of capital letters, the editor will detect that you no comment for this variable / class. This time there are two options

  1. The uppercase letters to lowercase letters service
  2. Annotation for a specific format, such as the above comment is//Service 服务类

In fact, in addition to this there is a practice of setting vscod direct correlation detection attributes "go.lintFlags":["--disable=all"], so do not write those annoying comments friends.

golang coding practices have a very interesting, it is that it's all the variables, methods, classes, and so do not need to score all the code number in the last sentence, even if you hit a semicolon, as the editor will automatically give you omitted

How to import third-party libraries?

Directly select the modules you want to import the official address of the warehouse, see Address: https://pkg.go.dev

import ("moduleName")

But I had to do the time, found that although the compiler does not detect the error, but in reference to api module, there is no intelligence tips. Later found with nuget is the same, there are shortcuts to import module: shift + command + pselect Go: Add Importcheck your local clone down the third-party libraries. But there is a question, do you want to download the source code for it every time? But can not directly download an executable similar dll "micro-Files" Mody. Reason is certainly there, a project can not release out, other people's source code also unable to go. After the release of this time to get back to check the information in it

Error 1: expected ';', found fThis is a different issue coded editor, I am using vscode take an example, the default encoding I go new project is LF/CRLFswitched to CRLF/LF. Note that if the switch when found or reported the same mistakes, it is very likely that vscode did not react, as long as the current page easily lose a space can be saved.

Then there is the basic function of usage of various variables introduced

null value: golong with representatives nil null, this is very special, ah, most languages ​​are null

The definition of class: The type keyword type ClassName struct{ someField int}

Declare variables: var scopeVar = "string"I tried it, the wording of this display is also OK var scopeVar string = "".

But there is such a kind of writing localVar := "", but also very interesting, I tried it, this seems to only write (which is equivalent to the method var localVar string = ""), such an approach to enhance the "global" is not.

concept with pointers golong

Stored pointer is the memory address of a variable. Such as var p *intp represents the address of the plastic, its zero value is nil

This with pointers c ++ is the same, more complex, I was in class on to this place when it is senseless to college, and "pointer", "pointer references," "pointer pointer", "*", "& "and always confused. In the next era with vc ++ 6.0, write code without any prompting, is simply impossible and.

But now the times are different now, ide / editor can automatically help you make the right choice. The chance to learn golang, but I still need to figure out the place of this knowledge.

First look at the code below, I write in the margin of the Notes

var p *int  // 变量 p 代表是整形的内存地址
i := 11 // 就一般的变量赋值
p = &i  // 给内存地址指针变量 p 赋值 11 的指针变量
*p = 1  // p 地址的值赋值为 1

The second line I will not explain.

The first line of code is a pointer to the variable p shaping memory address.

The third line of code that you give a plastic address assignment, it is certainly not a direct assignment integer i, but the variable i points to an integer address & i. In fact, it can be understood as a reference to the i.

I'm going straight to the fourth line p pointer to address the specific values ​​that we said before "pointers to pointers: * p = 1".

Declaring functions

Inside the same function in golang js is the same - first-class citizens. Whether you write that is where it all can be effective and can be referenced in the current domain.

Declaring functions in two ways

  1. None Return Value:func SomeMethod() {}
    1. With parameters:func SomeMethod(a int) {}
  2. Return value:func SomeMethodAndReturn() ReturnValue {}

Function and there are a fun convention:

  • The first letter of the function name is lowercase is private private methods
  • The first letter is uppercase function name is public sharing method

We can also set a class of functions (functions like the equivalent of C # delegate, delegate for the CLR is in terms of a class containing such a function, can also be used as internal handling in Java). Examples of code shown below

type delegateFunc func(string)  // 定一个委托
func serve(msg string){
  fmt.Printf(msg)
}
func main(){
  d := delegateFunc(serve)  // 把函数当作参数传递
  d("marson shine")
}

Method definition

Defined functions with methods like: func (type类型参数) MethodName(parameters) ReturnValue {}

First look at the official website of the definition of the method :

一个 type 指定的类型可以关联方法集。一个接口类型的方法集是其接口。任何类型 T 的方法集,由它作为接收器接收所有方法。对应的指针类型 *T 的方法集是由接收器 *T 或者是 T 申明的方法集(那也就说,它包含了 T 的所有方法集)。更多的规则运用在包含那些匿名字段的结构(struct)上。任何类型都有空的方法集。在一个方法集中,每个方法必须有一个唯一的不为空的名称。

We give an example to illustrate:

func (typeName ClassName) MethodName(parameter string) string {

}

Here we define a method, specifying the type of receiver is ClassName, that we have to have receivers to have this collection method MethodName

type ClassName struct {
        userName string
}

Well, this time I can honestly out a ClassName, then you can call the method MethodName.

For the method defined above, in fact, there is a written like this:

func (typeName *ClassName) MethodName(parameter string) string {

}

I read at this data and found that this is very interesting, with the "functional programming" features compilers and related golang itself. Functional has a very important feature is the "non-state". For example, I created a function that itself is stateless, as long as you pass parameters unchanged, the function value is obtained by a constant value. Our previous methods take as an example func (typeName ClassName) MethodName(parameter string) string {}this means that you can not change typeName this value, it is not variable. You can only change in this function field, once this method returns (specified stack address) is passed on or before typeName state. If you want to pass as C # as a reference, after the return of local changes, this will also change the reference object, then change how to achieve it? It is also very simple, as long as the original basis plus a "*", which is above the wording.

Guess you like

Origin www.cnblogs.com/ms27946/p/12389405.html