Martin Sulzmann
Each student will be assigned to one project (via a lottery).
Each project consists of a simple Go program (about 20 lines of code, similar complexity to the programs in the lecture notes).
Your task is to explain and modify the program and answer some questions.
Each student has to give an in-person presentation (about 5-10 minutes) and answer some additional questions.
The in-person presentation takes place in class (Tuesday 11:30-13:00 in E304) in week W14 and week W15.
package main
import "fmt"
type pair struct {
x int
y int
}
func inc(p pair) {
p.x++
}
func main() {
p := pair{1, 2}
inc(p)
fmt.Println(p.x)
}Tasks
package main
import "fmt"
func modify(xs []int) {
xs[0] = 42
}
func main() {
a := []int{1, 2, 3}
modify(a)
fmt.Println(a[0])
}Tasks
package main
import "fmt"
func modify(a [3]int) {
a[0] = 99
}
func main() {
x := [3]int{1, 2, 3}
modify(x)
fmt.Println(x[0])
}Tasks
package main
import "fmt"
func main() {
xs := []int{1, 2, 3}
for _, v := range xs {
v++
}
fmt.Println(xs)
}Tasks
Tasks
package main
import "fmt"
func main() {
xs := []int{1, 2}
ys := xs
xs = append(xs, 3)
xs[0] = 99
fmt.Println(ys[0])
}Tasks
package main
import "fmt"
func makeAdder(x int) func(int) int {
return func(y int) int {
return x + y
}
}
func main() {
add2 := makeAdder(2)
fmt.Println(add2(3))
}Tasks
package main
import "fmt"
type box struct {
value int
}
func update(b box, xs []int) {
b.value = 10
xs[0] = 10
}
func main() {
b := box{1}
xs := []int{1, 2}
update(b, xs)
fmt.Println(b.value, xs[0])
}Tasks