You are on page 1of 10

Programming in Go(lang)

Mar 28, 202


Go
 An open-source programming language created by Google
 Resolves common criticisms of other languages while maintaining
positive characteristics
 Created out of founder’s dislike of C++

 Compiled, concurrent, and imperative


 Does not require tooling, but has sufficient support
 Readable with few keywords or reputation (like other object-oriented
languages)
 Support networking and multiprocessing

 Hello World: package main


import "fmt"
func main() {
fmt.Println("Hello, World")
}
2
Language Design
 Similar to the design of C with improved brevity and simplicity
 Syntax and environment adopts patterns common in dynamic
languages
 Concise variable declaration and initialization with type inference
x := 0 versus var x int = 0;
 Fast compilation times, remote package management (go get) and online
documentation (similar to Oracle docs)
 Other notable design features / relevant approaches
 Built-in concurrency primitives achieve light-weight processes
(goroutines) and channels (select statement)
 Interface system (vs virtual inheritance) and type embedding (vs non-
virtual inheritance)
 Toolchain produces statically linked native binaries without external
dependencies

3
Syntax
 Combined declaration/initialization operator allows programmer
to write without specifying types of variables
i := 3  versus  int i = 3;   
s := "some words” versus  const char *s = "some words”
 Semicolons are implicit at the end of a line
 Functions may return multiple values
 Returning a result, err pair indicates an error to its caller
 Literal syntaxes for initializing struct parameters by name, and
for initializing maps and slices
 Go's range expressions allow concise iteration over arrays, slices,
strings, maps and channels (versus a three statement for loop)
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
} 4
Types
 Strong, Static, Inferred, Structural
 Built-in types like C++, Java
 Built-in operators and keywords (rather than functions) provide
concatenation, comparison, and UTF-8 encoding / decoding
 Record types be defined with the struct keyword
 For each type T and each non-negative integer constant n, there is
an array Type denoted [n]T
 Arrays of differing lengths are of different types
 Dynamic Arrays available as "slices", denoted []T for some type T
 Length & capacity specify when new memory needs to be allocated to

expand the array


 Several slices may share their underlying memory

letters := []string{"a", "b", "c", "d"}


5
More Types
 Pointers available for all types, and the pointer-to-T type is
denoted *T
 Address-taking and indirection use the & and *operators as in C or happen
implicitly through method calls / attribute access syntax
 There is no pointer arithmetic, except via the special unsafe.Pointer type
func Float64bits(f float64) uint64 {
return *(*uint64)(unsafe.Pointer(&f))
}
 Type system is nominal: the type keyword can be used to define
a new named type, which is distinct from other named types that
have the same layout
 Some conversions between types are pre-defined
 Adding a new type may define additional conversions, but conversions
between named types must always be invoked explicitly For example

6
Concurrency with GoRoutines
 Built-in facilities and library support for concurrent programs 
 CPU parallelism and asynchrony 
 The primary concurrency construct is  goroutine
 Function call prefixed with go keyword starts a function in new goroutine
 Current implementations multiplex a Go process's goroutines onto a
smaller set of operating system threads operating systems threads
  Similar to the scheduling performed in Erlang
func main() {
t := make(chan bool)
go timeout(t)

ch := make(chan string)
go readword(ch)

select { …
}} 7
Developer Criticism
 Lacks compile-time generics
 Results in code duplication
 Metaprogramming cannot be statically checked
 Standard library cannot offer generic algorithms
 Lack of language extensibility makes certain tasks more verbose
 Lacks operator overloading (Java)
 Type system lacks Hindley-Milner typing
 Inhibits safety and/or expressiveness
 Pauses and overhead of garbage collection
 Limit Go’s use in systems programming compared to languages with
manual memory management
 Mitigates: Designers argue these trade-offs are important to
Go’s success, and explain some particular decisions at length

8
Practical Use Cases
 The Really Good: Network and Web Servers
 Network applications live and die by concurrency
 Go's native concurrency features: (goroutines and channels)
 Networking, distributed functions, or services (APIs, Web Servers,
minimal frameworks for Web Apps)
 Items are either a part of language (goroutines for threadlike behavior) or
available in the standard library http package.
 ”Batteries included" philosophy like Python

 Stand-alone command-line apps or scripts


 Stand-alone executables with no external dependencies
 Versus Python which requires a copy of the interpreter on the target machine
 Can talk to underlying system, external C libraries, native system calls
 Caution: Desktop/GUI-based apps & System-Level
Programming

9
The End

10

You might also like