Lecture 05-Introduction To Golang
Lecture 05-Introduction To Golang
Introduction to Golang
1
Lecture 05 - Introduction to Golang
• Go is an open source
programming language that
makes it easy to build simple,
reliable, and efficient software.
2
Lecture 05 - Introduction to Golang
3
Lecture 05 - Introduction to Golang
Go at Google
• Go is a programming language designed by Google to
help solve Google's problems, and Google has big
problems
• The hardware is big and the software is big.
– many millions of lines of software, with servers mostly in C++
and lots of Java and Python for the other pieces.
– Thousands of engineers work on the code, at the "head" of a
single tree comprising all the software,
– From day to day there are significant changes to all levels of the
tree.
– A large custom-designed distributed build system makes
development at this scale feasible, but it's still big.
4
Lecture 05 - Introduction to Golang
Go at Google
• And of course, all this software runs on zillions of
machines, which are treated as a modest number of
independent, networked compute clusters.
5
Lecture 05 - Introduction to Golang
Pain points
• Slow builds
• Uncontrolled dependencies
• Each programmer using a different subset of the
language
• Poor program understanding (code hard to read, poorly
documented, and so on)
• Duplication of effort
• Cost of updates
• Difficulty of writing automatic tools
• ….
6
Lecture 05 - Introduction to Golang
7
Lecture 05 - Introduction to Golang
8
Lecture 05 - Introduction to Golang
9
Lecture 05 - Introduction to Golang
10
Lecture 05 - Introduction to Golang
11
Lecture 05 - Introduction to Golang
12
Lecture 05 - Introduction to Golang
13
Lecture 05 - Introduction to Golang
14
Lecture 05 - Introduction to Golang
15
Lecture 05 - Introduction to Golang
16
Lecture 05 - Introduction to Golang
17
Lecture 05 - Introduction to Golang
18
Lecture 05 - Introduction to Golang
Dependencies in Go
import "encoding/json"
19
Lecture 05 - Introduction to Golang
Dependencies in Go
package A imports package B;
package B imports package C;
package A does not import package C
20
Lecture 05 - Introduction to Golang
Dependencies in Go
• To build this program,
– first, C is compiled; dependent packages must be built
before the packages that depend on them.
– Then B is compiled; finally A is compiled, and then the
program can be linked.
21
Lecture 05 - Introduction to Golang
Results
• Google measured the compilation of a large
Google program written in Go to see how the
source code fanout compared to the C++
analysis done earlier.
• They found it around fifty times better than C++
(as well as being simpler and hence faster to
process)
22
Lecture 05 - Introduction to Golang
23
Lecture 05 - Introduction to Golang
Installing Go
• The Go Playground, http://play.golang.org (
https://go.dev/play/ )
24
Lecture 05 - Introduction to Golang
Installing Go
25
Lecture 05 - Introduction to Golang
https://go.dev/
26
Lecture 05 - Introduction to Golang
Installing Go
• You should however install it locally
– Follow instructions at https://go.dev/doc/install, simple steps and
binaries provided.
– Download and install using the setup
– The GOPATH environment variable should be setup
27
Lecture 05 - Introduction to Golang
28
Lecture 05 - Introduction to Golang
Hello World !!
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
29
Lecture 05 - Introduction to Golang
A web server !!
package main
import (
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8000", nil)
}
30
Lecture 05 - Introduction to Golang
31
Lecture 05 - Introduction to Golang
Go and GitHub
• The go tool is designed to work with open
source code maintained in public repositories.
32
Lecture 05 - Introduction to Golang
Workspaces
• Go code must be kept inside a workspace.
• A workspace is a directory hierarchy with three
directories at its root:
– src contains Go source files organized into packages (one
package per directory),
– pkg contains package objects, and
– bin contains executable commands.
An example
34
Lecture 05 - Introduction to Golang
35
Lecture 05 - Introduction to Golang
36
Lecture 05 - Introduction to Golang
37
Lecture 05 - Introduction to Golang
GitHub
38
Lecture 05 - Introduction to Golang
39
Lecture 05 - Introduction to Golang
• When you’re ready to share your code with others in your team, or
want to get it ready for deployment to your servers, you can “push” it
to your "remote"
40
Lecture 05 - Introduction to Golang
Basic Workflow
Basic Git workflow:
•Notes:
– If a particular version of a file is in the git directory, it’s considered committed.
– If it’s modified but has been added to the staging area, it is staged.
– If it was changed since it was checked out but has not been staged, it is modified.
42
Lecture 05 - Introduction to Golang
43
Lecture 05 - Introduction to Golang
44
Lecture 05 - Introduction to Golang
• You can call git config –list to verify these are set.
• These will be set globally for all Git projects you work with.
• You can also set variables on a project-only basis by not using the
--global flag.
45
Lecture 05 - Introduction to Golang
46
Lecture 05 - Introduction to Golang
Git commands
command description
git clone url [dir] copy a git repository so you can add to it
git add files adds file contents to the staging area
git commit records a snapshot of the staging area
git status view the status of your files in the working
directory and staging area
git diff shows diff of what is staged and what is
modified but unstaged
git help [command] get help info about a particular command
git pull fetch from a remote repo and try to merge
into the current branch
git push push your new branches and data to a
remote repository
others: init, reset, branch, checkout, merge, log, tag
47
Lecture 05 - Introduction to Golang
Committing files
• The first time we ask a file to be tracked, and every time before we
commit a file we must add it to the staging area:
• This takes a snapshot of these files at this point in time and adds it to the
staging area.
• To move staged changes into the repo we commit:
Note: These commands are just acting on your local version of repo.
48
Lecture 05 - Introduction to Golang
49
Lecture 05 - Introduction to Golang
Viewing logs
To see a log of all changes in your local repo:
• $ git log or
• $ git log --oneline (to show a shorter version)
50
Lecture 05 - Introduction to Golang
51
Lecture 05 - Introduction to Golang
52
Lecture 05 - Introduction to Golang
53
Lecture 05 - Introduction to Golang
A Tour of Go
• We would be following the online tour
– https://tour.golang.org or go tool tour
54
Lecture 05 - Introduction to Golang
Hello World !!
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
55
Lecture 05 - Introduction to Golang
56
Lecture 05 - Introduction to Golang
57
Lecture 05 - Introduction to Golang
58
Lecture 05 - Introduction to Golang
59
Lecture 05 - Introduction to Golang
A tour of Go
• Packages/Imports/Exported
names/Functions/Variables/Initializers
60
Lecture 05 - Introduction to Golang
61
Lecture 05 - Introduction to Golang
package main
import (
"fmt"
"github.com/user/greetings"
)
func main() {
fmt.Printf(greetings.PrintGreetings("ad "))
}
62
Lecture 05 - Introduction to Golang
63
Lecture 05 - Introduction to Golang
Remote repositories
• It is good time to push our package to the GitHub,
• Anyone can then use our wonderful greetings package
64
Lecture 05 - Introduction to Golang
Remote repositories
• Lets delete our local repo and get it from GitHub
• This shows how to use remote repos in the Go
go get github.com/user/greetings
65
Lecture 05 - Introduction to Golang
Remote repositories
• Lets do some image processing …
go get -u github.com/disintegration/imaging
…and we have an image processing library
package main
import (
"github.com/disintegration/imaging"
)
func main() {
img, _ := imaging.Open("01.jpg")
thumb := imaging.Thumbnail(img, 100, 100,
imaging.CatmullRom)
imaging.Save(thumb, "dst.jpg")
}
66
Lecture 05 - Introduction to Golang
Arrays
• Go's arrays are values.
• An array variable denotes the entire array; it is not a
pointer to the first array element (as in C/C++).
67
Lecture 05 - Introduction to Golang
Passing Arrays
• Go's documentation makes it clear that
arrays are passed by copy.
68
Lecture 05 - Introduction to Golang
Slices
• Slices build on arrays to provide great power and
convenience.
• A slice literal is declared just like an array literal, except
you leave out the element count:
var s []byte
s = make([]byte, 5, 5)
// s = []byte{0, 0, 0, 0, 0}
69
Lecture 05 - Introduction to Golang
Slicing Slices
b := []byte{'g', 'o', 'l', 'a', 'n', 'g'}
70
Lecture 05 - Introduction to Golang
Slice Internals
• A slice is a descriptor of an array segment. It consists of a pointer to
the array, the length of the segment, and its capacity (the maximum
length of the segment).
71
Lecture 05 - Introduction to Golang
72
Lecture 05 - Introduction to Golang
73
Lecture 05 - Introduction to Golang
Erratum – Go Modules
• The support of go modules was added in
v1.11 but wasn’t enforced for $GOPATH
packages till recent versions.
74
Lecture 05 - Introduction to Golang
GO111MODULE
• If you have made the change as announced on the
classroom, you need to undo. It should be on or empty
go env
75
Lecture 05 - Introduction to Golang
Go modules - Introduction
• They serve many purposes
– Dependency management, using multiple
versions of the downloaded packages
– Packages from go get are not cluttered in
$GOPATH/src and go code can be written in
directories other than $GOPATH
–…
76
Lecture 05 - Introduction to Golang
77
Lecture 05 - Introduction to Golang
78
Lecture 05 - Introduction to Golang
Getting started
• Go modules are initialized by the go init command
followed up by the path of the module
79
Lecture 05 - Introduction to Golang
80
Lecture 05 - Introduction to Golang
Git tags
• Using git we can tag our releases (provide versions) and
push on the github.
• We call our first release v0.0.1
81
Lecture 05 - Introduction to Golang
82
Lecture 05 - Introduction to Golang
package main
import (
"fmt"
"github.com/ehteshamz/greetings"
)
func main() {
fmt.Printf(greetings.PrintGreetings("ez "))
}
83
Lecture 05 - Introduction to Golang
84
Lecture 05 - Introduction to Golang
Adding dependency
• We have not added dependency for our main package that it needs
the greeting module.
• This can be done by editing the go.mod file or just running go get in
the same folder
• You can notice that we are adding version info with the package and
how the contents of go.mod change
85
Lecture 05 - Introduction to Golang
Adding dependency
*If you receive an error while running the go get command with text
including “incorrect version” and ”could not read Username” you need
to set an environment variable
go env -w GOPRIVATE=github.com/yourusername
What does it say? It says our repository is private and go should not
use the proxy service (goproxy.io) check its validity by checksum.
86
Lecture 05 - Introduction to Golang
87
Lecture 05 - Introduction to Golang
88
Lecture 05 - Introduction to Golang
89
Lecture 05 - Introduction to Golang
90
Lecture 05 - Introduction to Golang
module somename/somepackage
go 1.17
replace github.com/ehteshamz/greetings => ../greetings
require github.com/ehteshamz/greetings v0.0.2 // indirect
• What does it say?
91
Lecture 05 - Introduction to Golang
Greetings in Pashto
• We change the local version to this but do NOT push this
to github as say v0.0.3
92
Lecture 05 - Introduction to Golang
go run callgreetpackage.go
ez-سالم نړی
93