golang 101 for itpros - clnv.s3.amazonaws.com · golang 101 for itpros patrick riel,...

49

Upload: phungthuan

Post on 30-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

GoLang 101 for ITPros

Patrick Riel, [email protected]

Stève Sfartz, [email protected]

DEVNET-1808

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Cisco Spark

Questions? Use Cisco Spark to communicate with the speaker after the session

1. Find this session in the Cisco Live Mobile App

2. Click “Join the Discussion”

3. Install Spark or go directly to the space

4. Enter messages/questions in the space

How

cs.co/ciscolivebot#DEVNET-1808

• Why Golang ?

• Hello World (in Python / Golang in VSCode)

• Setting up your dev env, code formating, compiling, VSCode

• CLI example, Mac/Win/Linux binaries

• Advanced: goroutines & channels

• Deployment: docker packaging

Agenda

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Why Go?

• Released by Google in 2009

• conceived in 2007, popular since 2015

• Designed to address software engineering issues faced in the construction of large server software

• Influenced by: C, Python, Smalltalk, Alef, CSP, Modula, Pascal, Limbo, BCPL

• Focus on clarity, simplicity and composability

• resulted in a productive, fun, expressive and powerful language

• No class inheritance but ‘Embedding’ & ‘Interfaces’ to provide polymorphism

• Built-in concurrency (parallelism & asynchrony) via ‘Channels’ & ‘goroutines’

• Includes a - debugging, profiling, formatting and testing - toolset

5DEVNET-1808

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 6DEVNET-1808

Software engineering guided the design of Go

• Clear dependencies

• Clear syntax

• Clear semantics

• Composition over inheritance

• Simplicity provided by the programming model (garbage collection, concurrency)

• Easy tooling (the go tool, gofmt, godoc, gofix)

• TRY IT: https://tour.golang.org

https://talks.golang.org/2012/splash.article

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 7DEVNET-1808

Variableshttps://tour.golang.org/basics

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 8DEVNET-1808

Functions: mutiple & named resultshttps://tour.golang.org/basics/6

Go Tour

Setting up go on your machine

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1808

Installing Go

• Download the PKG on Mac (or MSI for Windows), and Install

• by default installs in /usr/local/go (or c:\Go for Windows).

• adds the Go binary to your PATH environment variable

• Set the GOROOT environment variable if you install to a different location

• Type ‘go version’ to check your installation is successful

• Create a Go workspace directory

• under $HOME/go on Mac (or %USERPROFILE%\go for Windows)

• Set a GOPATH environment variable is preferable (and mandatory if elsewhere)

• Add the $GOPATH/bin directory to your path

https://golang.org/doc/install

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 12DEVNET-1808

Hello World (on Windows)

• Create a “src/hello” directory in your workspace

• Create a file named hello.go

• Build it with the go tool

• Produces an executable named hello.exe

• Run go install to install the binary into your workspace's bin directory or go clean to remove it.

1

2

3

File: src/hello/hello.go

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 13DEVNET-1808

Hello World (on a Mac)

• Create a “src/hello” directory in your workspace

• Create a file named hello.go

• Build it with the go tool

• Produces an executable named hello.exe

• Run go install to install the binary into your workspace's bin directory or go clean to remove it.

1

File: src/hello/hello.go2

3

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 14DEVNET-1808

Typical go folder structure

• go tooling requires to organize your code in a specific way

• Go programmers typically keep all their Go code in a single workspace.

• A workspace contains many version controlrepositories (managed by Git, for example).

• Each repository contains one or more packages.

• Each package consists of one or more Go source files in a single directory.

• The path to a package's directory determines its import path.

https://golang.org/doc/code.html

GOPATH = C:\Local\gowork

mkdir ‘$GOPATH/src/github.com/<username>/hello’

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 15DEVNET-1808

Creating a Library

> mkdir $GOPATH/src/github.com/<user>/stringutil

https://golang.org/doc/code.html

filename: reverse.go

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 16DEVNET-1808

Creating a Library

> mkdir $GOPATH/src/github.com/user/hello

https://golang.org/doc/code.html

filename: hello.go

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 17DEVNET-1808

Creating a Libraryhttps://golang.org/doc/code.html

> go install github.com/<user>/hello• the ‘stringutil’ package is automatically installed as well

• Go command executables are statically linked

Setting up your IDE

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1808

Go Extension for Visual Studio Code

Installs automatically at launchor manually via go get –v:

github.com/nsf/gocodegithub.com/uudashr/gopkgs/cmd/gopkgs

github.com/ramya-rao-a/go-outlinegithub.com/acroca/go-symbolsgolang.org/x/tools/cmd/guru

golang.org/x/tools/cmd/gorenamegithub.com/fatih/gomodifytags

github.com/haya14busa/goplay/cmd/goplaygithub.com/josharian/implgithub.com/rogpeppe/godef

golang.org/x/tools/cmd/godocsourcegraph.com/sqs/goreturnsgithub.com/golang/lint/golint

github.com/cweill/gotestsgithub.com/derekparker/delve/cmd/dlv

golang.org/x/tools/cmd/guru

19

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

VS Code golang extension: Installation Tips

• If any go tools dependency is missing, install it manually

• Make sure the $GOPATH/bin directory appears in your path

• To install the Delve debugger, type

> go get -u github.com/derekparker/delve/cmd/dlv

• If having issue with the debugger on Mac: ‘lldb-server’ not found, type

> xcode-select –install

20DEVNET-1808

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 21DEVNET-1808

readword.go

Demo

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 23DEVNET-1808

Pointershttps://tour.golang.org/moretypes/1

CLI with golangdoing chatops

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Methods and Interfaces

• Remember: a method is just a function with a receiver argument

• To implement an interface in Go

• Implement all the methods in the interface

25DEVNET-1808

https://tour.golang.org/methods

https://gobyexample.com/interfaces

https://github.com/ObjectIsAdvantag/CLEUR-1814/blob/master/7-spark/main.go

https://github.com/ObjectIsAdvantag/CLEUR-1814/blob/master/6-interfaces/main.go

CLI Demo

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 27DEVNET-1808

Deferhttps://tour.golang.org/flowcontrol/12

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Defer

• defers the execution of a function until the surrounding function returns

• deferred function calls are pushed onto a stack (executed in LIFO)

28DEVNET-1808

counting

done

5

4

3

2

1

0

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

HTTP API Clients

• https://www.scaledrone.com/blog/posts/creating-an-api-client-in-go

• simplify development

• speed up development

• clean code

• abstraction

DEVNET-1808 29

Concurrency

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 31DEVNET-1808

Channels & GoRoutines

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 32DEVNET-1808

Channels & GoRoutines

More about #golang

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 34DEVNET-1808

Structureshttps://tour.golang.org/moretypes/2

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 35DEVNET-1808

Arrayshttps://tour.golang.org/moretypes/6

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 36DEVNET-1808

Sliceshttps://tour.golang.org/moretypes/13

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 37DEVNET-1808

Mapshttps://tour.golang.org/moretypes/22

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 38DEVNET-1808

Closureshttps://tour.golang.org/moretypes/25

Deployment & packaging

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Packaging for various platforms

• The go toolchain natively cross-build executables for any Go-supported foreign platform

• Example:

> env GOOS=linux GOARCH=amd64 go build -o dist/demo.linux -v main.go

• https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04

DEVNET-1808 40

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1808

Dockerfile examplehttps://github.com/ObjectIsAdvantag/CLEUR-1814/tree/master/8-docker

41

Wrapup

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Wrapup… go for ITPros

CLI

Proxy

Reverse Proxy

Web API

43DEVNET-1808

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 44DEVNET-1808

Cisco Community Contributions

• golang samples

• https://github.com/ObjectIsAdvantag/CLEUR-1814

• Cisco Spark REST API wrapper

• https://github.com/jbogarin/go-cisco-spark

• Cisco ACI from golang

• https://github.com/udhos/acigo

• Answering Machine backed by Tropo

• https://github.com/ObjectIsAdvantag/answering-machine

• Proxy incoming web hooks to established web sockets

• https://github.com/sgrimee/whproxy

https://blog.golang.org

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Cisco Spark

Questions? Use Cisco Spark to communicate with the speaker after the session

1. Find this session in the Cisco Live Mobile App

2. Click “Join the Discussion”

3. Install Spark or go directly to the space

4. Enter messages/questions in the space

How

cs.co/ciscolivebot#DEVNET-1808

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

• Please complete your Online Session Evaluations after each session

• Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt

• All surveys can be completed via the Cisco Live Mobile App or the Communication Stations

Don’t forget: Cisco Live sessions will be available for viewing on-demand after the event at www.ciscolive.com/global/on-demand-library/.

Complete Your Online Session Evaluation

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Continue Your Education

• Demos in the Cisco campus

• Walk-in Self-Paced Labs

• Tech Circle

• Meet the Engineer 1:1 meetings

• Related sessions

47DEVNET-1808

Thank you