Professional Documents
Culture Documents
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
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
9
The End
10