码迷,mamicode.com
首页 > 其他好文 > 详细

视频版 |《老运维带你用 Golang 飞》

时间:2017-09-22 17:52:36      阅读:695      评论:0      收藏:0      [点我收藏+]

标签:互联网   运维 web开发 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


技术分享


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.

技术分享


  • 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, <-.

技术分享


  • Don‘t communicate by sharing memory, Share memory by communicating(No Lock !)

  • http://my.oschina.net/clopopo/blog/141873


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


  • A WaitGroup waits for a collection of goroutines to finish.

  • Add()

  • Wait()

  • Done()

  • https://github.com/wusuopubupt/go_spider/blob/master/src/spider/spider.go


Example


  • http://eleme.io/blog/2014/goroutine-1/

  • https://github.com/wusuopubupt/go_spider

  • Docker


Docker -- A Go Project


  • Docker enables developers and IT admins to build, ship and run any application, anywhere. 

  • written in the Golang, released as open source in 2013

  • Resource isolation(Namespaces, Cgroups, Libcontainer)

  • https://en.wikipedia.org/wiki/Docker_(software) 

  • https://www.docker.com/

  • http://www.infoq.com/cn/articles/docker-kernel-knowledge-cgroups-resource-isolation


Docker basic concept


  • Image (https://hub.docker.com)

  • container

  • repository


Dockerfile


技术分享


  • docker build -t 4paradigm/dango-base-lastest .     (-t == --tag)

  • docker run -t -i 4paradigm/dango-base-lastest /bin/bash  (-t == --tyy),  ip conflict?

  • See more: https://docs.docker.com/engine/reference/builder/


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


视频版


百度网盘链接地址:http://pan.baidu.com/s/1pL8zPcn

密码:e4vk 


本文出自 “Python & Golang 学习” 博客,请务必保留此出处http://51reboot.blog.51cto.com/2180875/1967772

视频版 |《老运维带你用 Golang 飞》

标签:互联网   运维 web开发 golang

原文地址:http://51reboot.blog.51cto.com/2180875/1967772

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!