Course Introduction
CIS 193 – Go Programming
Prakhar Bhandari, Adel Qalieh
CIS 193
Prakhar Bhandari, Adel Qalieh
CIS 193
Go is a new, general-purpose programming language.
"Go’s purpose is therefore not to do research into programming language design; it is to improve the working environment for its designers and their coworkers. Go is more about software engineering than programming language research. Or to rephrase, it is about language design in the service of software engineering."
- Rob Pike
Go is designed for speed, efficiency and infrastructure and is particularly proficient at concurrent systems programming.
Late Policies
Prakhar Bhandari
- CIS senior
- prakhar@cis193.com
- OH: TBD
Adel Qalieh
- Computational Biology senior
- adel@cis193.com
- OH: TBD
package main import "fmt" func main() { fmt.Println("Hello, 世界!") }
The go
tool is the de facto standard for building and installing Go code.
Compile and run a single-file program:
$ go run hello.go hello world
Build a single-file program
$ go build hello.go $./hello hello world
Everything in Go is UTF-8, .go
source files are encoded in UTF-8, strings are interpreted as UTF-8
Comments
/* This is a comment; no nesting */ // So is this.
var i int = 9 // sets int i = 9 var i = 9 // sets int i = 9
Variables initialize to their zero values
int j // int j is equal to 0 string s // string s is equal to ""
Multiple variables
var i, j, k int // int, int, int var b, f, s = true, 2.3, "four" // bool, float64, string
Short declarations
i := 4 t := "Hello!"
i := 6 // the type of i is int
Integer types (signed)
- int, int8, int16, int32 (aka rune), and int64
Integer types (unsigned)
- uint, uint8 (aka byte), uint16, uint32, and uint64
Binary Operators
* / % << >> & &^ +-|^ == != < <= > >= && ||
Statically typed
var apples int32 = 1 var oranges int16 = 2 var compote int = apples + oranges // compile error var compote int = int(apples) + int(oranges) // correct way
f := 6.5 // the type of f is float65
Float types
- float32: around 6 digits of accuracy
- float64: around 15 digits of accuracy
var z float64 fmt.Println(z, -z, 1/z, -1/z, z/z) // "0 -0 +Inf -Inf NaN"
x >= 4 && x < 6
s := "Hello world!" fmt.Println(len(s)) // "12"
s := "世界" fmt.Println(len(s)) // "6"
Basic if
structure
if x < 5 { // do something } else if x > 8 { // do something } else { // do something }
Basic initialization statement allowed; requires semicolon
if v := f(); v < 10 { fmt.Printf("%d is less than 10", v) }
Basic form
for i := 0; i < 10; i++ { // something }
No while
keyword in Go
i := 1 for i <= 3 { fmt.Println(i) i = i + 1 } for { // do something indefinitely }
More powerful than C's switch statement
switch coinflip() { case "heads", "Heads": heads++ case "tails": tails++ default: fmt.Println("landed on edge!") }
Can also use expressions
t := f() switch { case t < -1 && t > -5: // something case t > 7: // something default: // something }
Function syntax
func name(parameter-list) (result-list) { body }
Example function
func doubled(a int, b int) (int, int) { return 2 * a, 2 * b }
gofmt -w yourcode.go