Go Language Notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Learning about Docker will have to learn the go language. Especially the interface type, it is not very convenient to feel. Update your understanding a little bit.

The package in the 1.go language.
In Java, Direct is the class of gratitude, import is also directly to the class in effect. However, in the go language, directories and packages will be used accordingly.
Directory: Import is the imported directory.
Packege: There can be multiple go files in a directory, but only one directory.
In addition, the default package for the system has a specified path. @GOPATH/SRC, the imported packages start from this path. Look at the source of time or to pay attention to the point, or the compiler can not recognize

import"fmt"//go语言自带的包    fmt.Print()  //使用方式    import"google.golang.org/grpc"    grpc.NewServer()  //有时候看第三方的源码的时候就很不方便,只能全局搜索。    "xcl"//给目录下的包起个别名    

2. Simple syntax, just note that some things are similar to C + +, and some are similar to Python. Note that, in the go language, the imported package is not used, and it becomes error-prone.

定义:    varstring    var"hello"方法:    // func 关键字    // 紧接着属于某个定义的结构体的方法    // 方法名    // 参数列表    // 返回值,都是相反的,感觉还是很别扭的    func (s *server) Logout(ctx context.Context, uid *pb.UserID) (*pb.FuncResponse, error) {          return param1, param2 }  结构体:类似于C语言吧,还有指针的概念。typestruct{}

3.go get can download and install the remote code base, which is downloaded by default in the @gopath/src directory.

go get google.golang.org/grpcexport PATH=$PATH:$GOPATH/bin //下载的第三方二进制文件,会默认在这个目录,使用的时候添加一下环境变量。

Use of 4.goroutine
One of the big advantages of the go language is that it is easy to write concurrent programs. The goroutine mechanism is built into the go language. But the process ends because the main function ends.

实现方式func test(){    fmt.Println("hello world")}go//使用go关键字来创建一个goroutine,类似于线程的使用

5.goroutine cannot block the main thread, it can be used in conjunction with channel. Pipelines are communication between goroutine that the go language provides at the language level, and we can use the channel to pass messages between multiple goroutine. Channel is the process of communication, is not support cross-process communication, if the need for interprocess communication, you can use the socket and other network methods.

 PackageMainImport "FMT"func Print(ChChan int) {FMT. Println ("Hello World") ch<-1}funcMain () {CHS: = Make([]Chan int,Ten)//Create a channel array     forI: =0; I <Ten; i++ {Chs[i] = Make(Chan int)Go Print(Chs[i])} for_, ch: =Range(CHS) {<-ch//If the data cannot be read, it will block}}

Reference:
[1] Go language primer
[2] Summary of the pipeline (channel) in the Go language
[3] Understanding of goroutine concurrent processing tasks for Golang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.