今晚九点 |《老运维带你用 Golang 飞》

本次公开课主题:《老运维带你用 Golang 飞》

Outline

  • What is Golang
  • Syntax
  • Concurrent
  • Example
  • Docker
  • Useful tools

History

  • Design began in late 2007 by Google.
  • Authors:
    Rob Pike (Bell Lab & UTF-8)
    Ken Thompson (UNIX & C & Turning Award)
    Robert Griesemer (Chrome JavaScript V8)

Go Users

Google, Facebook, Github, Dropbox, Docker, CloudFlare, DigitalOcean, Baidu BFE, 许式伟…

https://github.com/golang/go/wiki/GoUsers

What is Go?

  • open source
  • concurrent
  • garbage-collected
  • efficient
  • scalable
  • simple
  • http://golang.org
  • benchmark

Declarations

  • C syntax
  • Go syntax

For more information:

  • golang.org/s/decl-syntax
  • Function syntax
  • Function on type T:
  • Method of type T (Class Method?):
  • Variable (closure) of type T:
  • Multi return

Naming

  • Simple rule:
    upper case initial letter: Name is visible to clients of package
    otherwise: name (or _Name) is not visible to clients of package
  • Applies to variables, types, functions, methods, constants, fields....
  • That Is It.

Pointers and Structs

  • Go has pointers. A pointer holds the memory address of a variable.

Reflection

图片8.png

Garbage collection

  • In C:

Auto GC (Mark and Sweep), stop-the-world(-_-)

see more: http://adamansky.bitbucket.org/slides/gc/index.html?full#1
Problems:

  • Memory leak or Wild pointer ?

Defer

  • A defer statement defers the execution of a function until the surrounding function returns.
图片12.png
  • Defer Stack

Concurrent

  • In C:
    process, thread, libevent

  • In Go:
    goroutine
    channel
    select
    waitGroup

Goroutine

  • A goroutine is a lightweight thread managed by the Go runtime.
  • Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives. (channel & select)
  • See more: https://tour.golang.org/concurrency/1
  • go func() {}
  • coroutine Python?

Channel

  • Channels are a typed conduit through which you can send and receive values with the channel operator, <-.

Dead lock

Buffered channel

Buffered channel (like Queue in Python, thread safe FIFO)
Sends to a buffered channel block only when the buffer is full.
Receives to block when the buffer is empty.

Select

  • The select statement lets a goroutine wait on multiple communication operations.
  • A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
  • The default case in a select is run if no other case is ready.

WaitGroup

Example

Docker -- A Go Project

Docker basic concept

Dockerfile

Dokcer Commands

  • Image
  • docker images
  • docker build
  • docker rmi <image name>
  • docker export
  • Container
  • docker ps -a
  • docker run/restart/start/stop <container name>
  • docker logs < container name>
  • docker exec -ti $1 bash
  • Repository
  • docker login
  • docker search
  • docker push/pull
  • https://docs.docker.com/engine/reference/commandline/

Useful tools 4 Go

  • gofmt
  • go build|run|get|test
  • godoc
  • vim + YCM

What's next?

  • Try It!
  • tour.golang.org
  • golang.org/wiki/Learn
  • golang.org/project

技术交流QQ群:426582602
加入QQ群,获取分享直播链接和相关资料.
咨询报名联系:

QQ(1):979950755 小月
QQ(2):279312229 ada
WeChat : 1902433859 小月
WeChat : 1251743084 小单

Golang 实战班第2期火热报名进行中

招生要求:

有Linux基础,有志于使用 Go 语言做分布式系统编程的人员,想往系统架构师方向发展的同学。BAT 架构师带你一起飞。

开课时间:10月14日

课程内容:

  • Golang入门
  • Golang程序结构
  • Golang的基础数据类型
  • Golang复合数据类型
  • Golang的函数
  • Golang的方法
  • Golang的接口
  • Golang的协程和Channel
  • Golang基于共享变量的并发
  • Golang包和工具

课程大纲:http://51reboot.com/course/go/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 你来,我认真待你,你走,我目送你离开! 不强求,随遇而安。彼此安好,如此而已! 愿你一切安好。我们随缘就好!
    希雅的花园阅读 1,423评论 0 0
  • 其实没人能告诉你该怎么办,因为没有人是你自己,只有你才能为自己的人生负责。所谓的人生大赢家,并不在于你在哪里、做什...
    Uynahhruzuy阅读 1,053评论 0 0
  • #刘洲成家暴#被各大网络平台曝光之后,那首红极一时的“你就是我心中的棉花糖”变成了“你就是我心中的大智障”...
    漠晓星阅读 3,237评论 0 4