Learning Go From Hello
World to Web Service
Eric Potter
Sweetwater
Learning Go From Hello
World to Web Service
Eric Potter
Sweetwater
G
e
t
G
o
i
n
g
3
Learning Go From Hello
World to Web Service
Eric Potter
Sweetwater
Agenda
G
e
t
G
o
i
n
g
6
1:00 Intro, Background, Tooling, Basic Syntax
1:30 Exercises
2:00 Arrays / Slices, Types / Interfaces
2:30 Exercises
3:00 Go Routines
3:30 Exercises
4:00 Web Services
4:30 Exercises
Background
Before we go
7
Creators
Rob Pike
• Worked at Bell
Labs and Google
• co-creator of
UTF-8
Ken Thompson
• Designed and
implemented the
original Unix
Robert Griesemer
• Worked on
Google's V8
JavaScript engine
G
e
t
G
o
i
n
g
8
Fast
G
e
t
G
o
i
n
g
9
G
e
t
G
o
i
n
g
10
Tools
All systems go
11
Installation
P
R
E
S
E
N
T
A
T
I
O
N
T
I
T
L
E
12
Go CLI
• go run
• go build
• go mod init
• go test
• go get
• go fmt
• etc
G
e
t
G
o
i
n
g
13
IDEs
VS Code GoLand ($)
G
e
t
G
o
i
n
g
14
Hello World
Demo
G
e
t
G
o
i
n
g
15
Hello World
G
e
t
G
o
i
n
g
16
Imports
Single Multiple
G
e
t
G
o
i
n
g
17
Types
Go your own way
18
Basic Types
• bool
• string
• int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64
• rune // alias for int32 that represents a Unicode code point
• float32, float64
• complex64, complex128
G
e
t
G
o
i
n
g
19
Variable Declaration
G
e
t
G
o
i
n
g
20
Syntax
Let it go
21
Terse
; ( )
G
e
t
G
o
i
n
g
22
Notable Omissions
++i
do {}
while
G
e
t
G
o
i
n
g
23
Function Syntax
C#
public <returnType> <name> (<args>)
Go
func <name> (<args>) <returnType>
G
e
t
G
o
i
n
g
24
Fizz Buzz
Demo
G
e
t
G
o
i
n
g
25
Fizz Buzz
G
e
t
G
o
i
n
g
26
Questions
G
e
t
G
o
i
n
g
27
Exercise 1
Euler 1
1) If we list all the natural numbers below 10 that are multiples of 3
or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
2) The Sine value for 2 is positive and the value for 5 is negative.
Print the sum of the positive sine values from the input between 0
and 10. (hint import “math” and use math.Sin)
If you get done, try Euler 2 (https://projecteuler.net/problem=2)
G
e
t
G
o
i
n
g
28
Euler 1
G
e
t
G
o
i
n
g
29
G
e
t
G
o
i
n
g
30
Arrays
Go home
32
Declaring Arrays
G
e
t
G
o
i
n
g
33
Accessing Array Elements
G
e
t
G
o
i
n
g
34
G
e
t
G
o
i
n
g
35
Iterate over an Array
G
e
t
G
o
i
n
g
36
The size is part of the type
G
e
t
G
o
i
n
g
37
Slices
Go home
38
G
e
t
G
o
i
n
g
39
Dynamic Sizing
G
e
t
G
o
i
n
g
40
Ranges
G
e
t
G
o
i
n
g
41
Classes
Go home
42
Classes
Go home
43
Packages & Structs
Defining a Struct
G
e
t
G
o
i
n
g
44
G
e
t
G
o
i
n
g
45
G
e
t
G
o
i
n
g
46
Associating Functions with Structs
G
e
t
G
o
i
n
g
47
Interfaces
Go home
48
Defining an Interface
G
e
t
G
o
i
n
g
49
Checkerboard demo
Demo
G
e
t
G
o
i
n
g
50
Questions
G
e
t
G
o
i
n
g
51
Exercise 2
Create a GIF
Clone this repo: https://github.com/pottereric/GoGifBuilder
Using the image/gif and image/color packages, create a gif of a
checker board.
Using the same libraries, create an animated gif of a sine wave.
G
e
t
G
o
i
n
g
52
Concurrency
You go your way,
I’ll go mine
53
Concurrent Functions
Demo
G
e
t
G
o
i
n
g
54
G
e
t
G
o
i
n
g
55
Channels
Go change the
channel
56
TextEntryWithTimeout
Demo
G
e
t
G
o
i
n
g
57
TextEntryWithTimeout
G
e
t
G
o
i
n
g
58
WaitGroups
Go wait in the corner
59
G
e
t
G
o
i
n
g
60
Questions
G
e
t
G
o
i
n
g
61
Exercise 3
Create a GIF Concurrently
Modify your code that creates a sine wave gif to create the file
concurrently while the user enters the output file name.
G
e
t
G
o
i
n
g
62
G
e
t
G
o
i
n
g
63
G
e
t
G
o
i
n
g
64
Web Services
Go on
65
net/http Package
• The core functionality is in a standard library.
• There are packages like gin that provide structure.
G
e
t
G
o
i
n
g
66
net/http
G
e
t
G
o
i
n
g
67
net/http
G
e
t
G
o
i
n
g
68
Data Access
Go get it
69
database/sql
• Go uses a driver and plugin model.
• There are larger frameworks like Gorm
G
e
t
G
o
i
n
g
70
database/sql
G
e
t
G
o
i
n
g
71
Questions
G
e
t
G
o
i
n
g
72
Exercise 4
Web Services
Create a webservice that returns the sine of a given number.
Create a webservice that returns your sine wave GIF
G
e
t
G
o
i
n
g
73
Thank you
Eric Potter • @pottereric •
HumbleToolsmith.com
G
e
t
G
o
i
n
g
75

Learning Go From Hello World to Web Service.pptx

Editor's Notes

  • #2 I’m not a go expert Open C:\Projects\exploration\GO\MIDOTNET\TextEntryWithTimeout
  • #3 I’m not a go expert You are probably very good at your job. You write good code. But you may run into a situation where your services aren’t fast enough. You need a faster platform. That is where Go comes in. You can use go to build services that are fast and easy to maintain. This enables you to build software for your users that meets their needs and makes them successful.
  • #5 I’m not a go expert You are probably very good at your job. You write good code. But you may run into a situation where your services aren’t fast enough. You need a faster platform. That is where Go comes in. You can use go to build services that are fast and easy to maintain. This enables you to build software for your users that meets their needs and makes them successful.
  • #8 Batteries included, fast Modernize systems programming Concurrency Garbage Collection
  • #9 “The three of us got together and decided that we hated C++.” – Thompson First planned while waiting for C++ program to compile ('07) First stable released in '12
  • #12 CLI go mod init, mod tidy, go fmt, go build, go run, go test VS Code, Go Land
  • #14 Unit test runner, package manger, and formatter are built in
  • #16 CLI and VS Code If you don’t create a mod, you have to run the file name - go run helloWorld.go
  • #22 No parens, no semi-colons Func, for, if (no do or while loops)
  • #24 Generics only added recently
  • #25 In go, if the function name is capitalized, it is visible outside the module The return type can be a tuple, or 2 values
  • #26 VS CODE
  • #29 Hints Multiline imports Import “math” Math.Sin Cast to float64 – float64(i)
  • #32 Slices are a powerful abstraction on top of arrays Go has pointers but doesn’t allow pointer arithmatic
  • #33 The size is part of the type
  • #39 Slices store data in an underlying array
  • #42 The low end of the range is inclusive, the high end is exclusive
  • #44 Interfaces are the only abstract data type
  • #45 https://github.com/golang/go/blob/master/src/runtime/slice.go
  • #50 Go has structural-typing The programmer doesn’t have to declare and a struct is a reader, it just needs to implement the io.Reader and io.ByteReader inerfaces
  • #51 Todo – clone existing repo
  • #53 TODO Show how to write to a file
  • #54 Goroutine Managed by go runtime Channel Select Allow goroutine to read from channel(s)
  • #55 GoRoutinesDemo
  • #57 Channel Select Allow goroutine to read from channel(s)
  • #59 Go’s select lets you wait on multiple channel operations.
  • #60 Channel Select Allow goroutine to read from channel(s)
  • #63 If you get do
  • #68 See HelloNetHttp
  • #72 defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns Defer is used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup. defer is often used where e.g. ensure and finally would be used in other languages.
  • #74 If you get do