Learning Go From Hello
World to Web Service
Eric Potter
Learning Go From Hello
World to Web Service
Eric Potter
Background
Before we go
4
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
5
Fast
G
e
t
G
o
i
n
g
6
G
e
t
G
o
i
n
g
7
Tools
All systems go
8
Installation
P
R
E
S
E
N
T
A
T
I
O
N
T
I
T
L
E
9
Go CLI
• go run
• go build
• go mod init
• go test
• go get
• go fmt
• etc
G
e
t
G
o
i
n
g
10
IDEs
VS Code GoLand ($)
G
e
t
G
o
i
n
g
11
Hello World
Demo
G
e
t
G
o
i
n
g
12
Hello World
G
e
t
G
o
i
n
g
13
Types
Go your own way
14
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
15
Variable Declaration
G
e
t
G
o
i
n
g
16
Syntax
Let it go
17
Terse
; ( )
G
e
t
G
o
i
n
g
18
Notable Omissions
++i
do {}
while
G
e
t
G
o
i
n
g
19
Function Syntax
C#
public <returnType> <name> (<args>)
Go
func <name> (<args>) <returnType>
G
e
t
G
o
i
n
g
20
Fizz Buzz
Demo
G
e
t
G
o
i
n
g
21
Fizz Buzz
G
e
t
G
o
i
n
g
22
Questions
G
e
t
G
o
i
n
g
23
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 Math.Sin values 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.
If you get done, try Euler 2 (https://projecteuler.net/problem=2)
G
e
t
G
o
i
n
g
24
Euler 1
G
e
t
G
o
i
n
g
25
G
e
t
G
o
i
n
g
26
Arrays
Go home
28
Declaring Arrays
G
e
t
G
o
i
n
g
29
Accessing Array Elements
G
e
t
G
o
i
n
g
30
G
e
t
G
o
i
n
g
31
Iterate over an Array
G
e
t
G
o
i
n
g
32
The size is part of the type
G
e
t
G
o
i
n
g
33
Slices
Go home
34
G
e
t
G
o
i
n
g
35
Dynamic Sizing
G
e
t
G
o
i
n
g
36
Ranges
G
e
t
G
o
i
n
g
37
Classes
Go home
38
Classes
Go home
39
Packages & Structs
Defining a Struct
G
e
t
G
o
i
n
g
40
G
e
t
G
o
i
n
g
41
G
e
t
G
o
i
n
g
42
Associating Functions with Structs
G
e
t
G
o
i
n
g
43
Interfaces
Go home
44
Defining an Interface
G
e
t
G
o
i
n
g
45
Questions
G
e
t
G
o
i
n
g
46
Exercise 2
Create a GIF
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
47
Concurrency
You go your way,
I’ll go mine
48
Concurrent Functions
Demo
G
e
t
G
o
i
n
g
49
G
e
t
G
o
i
n
g
50
Channels
Go change the
channel
51
TextEntryWithTimeout
Demo
G
e
t
G
o
i
n
g
52
TextEntryWithTimeout
G
e
t
G
o
i
n
g
53
WaitGroups
Go wait in the corner
54
G
e
t
G
o
i
n
g
55
Questions
G
e
t
G
o
i
n
g
56
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
57
G
e
t
G
o
i
n
g
58
G
e
t
G
o
i
n
g
59
Web Services
Go on
60
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
61
net/http
G
e
t
G
o
i
n
g
62
Data Access
Go get it
63
database/sql
• Go uses a driver and plugin model.
• There are larger frameworks like Gorm
G
e
t
G
o
i
n
g
64
database/sql
G
e
t
G
o
i
n
g
65
Questions
G
e
t
G
o
i
n
g
66
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
67
Thank you
Eric Potter • @pottereric •
HumbleToolsmith.com
G
e
t
G
o
i
n
g
69

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 Batteries included, fast Modernize systems programming Concurrency Garbage Collection
  • #6 “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
  • #9 CLI go mod init, mod tidy, go fmt, go build, go run, go test VS Code, Go Land
  • #11 Unit test runner, package manger, and formatter are built in
  • #13 CLI and VS Code
  • #18 No parens, no semi-colons Func, for, if (no do or while loops)
  • #20 Generics only added recently
  • #21 In go, if the function name is capitalized, it is visible outside the module The return type can be a tuple, or 2 values
  • #22 VS CODE
  • #25 If you get do
  • #28 Slices are a powerful abstraction on top of arrays Go has pointers but doesn’t allow pointer arithmatic
  • #29 The size is part of the type
  • #35 Slices store data in an underlying array
  • #38 The low end of the range is inclusive, the high end is exclusive
  • #40 Interfaces are the only abstract data type
  • #41 https://github.com/golang/go/blob/master/src/runtime/slice.go
  • #46 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
  • #48 If you get do
  • #49 Goroutine Managed by go runtime Channel Select Allow goroutine to read from channel(s)
  • #52 Channel Select Allow goroutine to read from channel(s)
  • #54 Go’s select lets you wait on multiple channel operations.
  • #55 Channel Select Allow goroutine to read from channel(s)
  • #58 If you get do
  • #66 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.
  • #68 If you get do