Course Introduction

CIS 193 – Go Programming

Prakhar Bhandari, Adel Qalieh

CIS 193

What is Go?

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

Why use Go?

Go is designed for speed, efficiency and infrastructure and is particularly proficient at concurrent systems programming.

History of Go

Who uses Go?

Class Goals & Prerequisites

Topics Covered (tentative)

Syllabus and Grading

Homeworks

Late Policies

Academic Integrity

Class Structure and Resources

Instructors

Prakhar Bhandari
- CIS senior
- prakharb@seas.upenn.edu
- OH: TBD

Adel Qalieh
- Computational Biology senior
- adelq@seas.upenn.edu
- OH: TBD

Go Installation & GOPATH

Your First Go Program!

package main

import "fmt"

func main() {
    fmt.Println("Hello, 世界!")
}

Running Go Programs

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

Lexical Structure

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.

Variables

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!"

Basic Data Types

Integers

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

Floats

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"

Booleans

x >= 4 && x < 6

Strings

s := "Hello world!"
fmt.Println(len(s)) // "12"
s := "世界"
fmt.Println(len(s)) // "6"

Control Structures

If

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)
}

For

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
}

Switch

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
}

Functions

Function syntax

func name(parameter-list) (result-list) { 
    body
}

Example function

func doubled(a int, b int) (int, int) {
    return 2 * a, 2 * b
}

Packages

gofmt

gofmt -w yourcode.go

Homework 0

Thank you

Prakhar Bhandari, Adel Qalieh

CIS 193

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)