Before the Lab
Read
~/cs2613/labs/L19~/cs2613/labs/L19/functions.gopackage main
import "fmt"
// Here's a function that takes two `int`s and returns
// their sum as an `int`.
func plus(a int, b int) int {
// fill in a recursive definition of plus in the style of Lab 2
}
// When you have multiple consecutive parameters of
// the same type, you may omit the type name for the
// like-typed parameters up to the final parameter that
// declares the type.
func plusPlus(a, b, c int) int {
return plus(plus(a, b),c)
}
func main() {
// Call a function just as you'd expect, with
// `name(args)`.
res := plus(1, 2)
fmt.Println("1+2 =", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}
$ go run functions.go
1+2 = 3
1+2+3 = 6
minus function (recursively implemented in the same way, so that) the output looks like$ go run functions.go
1+2 = 3
7-3 = 4
1+2+3 = 6
~/cs2613/labs/L19/pointers.go// Go supports <em><a href="https://en.wikipedia.org/wiki/Pointer_(computer_programming)">pointers</a></em>,
// allowing you to pass references to values and records
// within your program.
package main
import "fmt"
// We'll show how pointers work in contrast to values with
// 2 functions: `zeroval` and `zeroptr`. `zeroval` has an
// `int` parameter, so arguments will be passed to it by
// value. `zeroval` will get a copy of `ival` distinct
// from the one in the calling function.
func zeroval(ival int) {
ival = 0
}
// `zeroptr` in contrast has an `*int` parameter, meaning
// that it takes an `int` pointer. The `*iptr` code in the
// function body then _dereferences_ the pointer from its
// memory address to the current value at that address.
// Assigning a value to a dereferenced pointer changes the
// value at the referenced address.
func zeroptr(iptr *int) {
*iptr = 0
}
func main() {
i := 1
fmt.Println("initial:", i)
zeroval(i)
fmt.Println("zeroval:", i)
// The `&i` syntax gives the memory address of `i`,
// i.e. a pointer to `i`.
zeroptr(&i)
fmt.Println("zeroptr:", i)
// Pointers can be printed too.
fmt.Println("pointer:", &i)
// A new pointer to a value can be created with the
// builtin function `new`.
p := new(42)
fmt.Println("value at *p:", *p)
zeroptr(p)
fmt.Println("value at *p:", *p)
}
main and define swapint, so that the output of the program matches that below *p=3
i=4
swapint(&i,p)
fmt.Println("after swap value at *p:", *p)
fmt.Println("after swap value in i", i)
$ go run pointers.go
initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x344372b720f8
value at *p: 42
value at *p: 0
after swap value at *p: 4
after swap value in i: 3
// Go's _structs_ are typed collections of fields.
// They're useful for grouping data together to form
// records.
package main
import "fmt"
// This `person` struct type has `name` and `age` fields.
type person struct {
name string
age int
}
// `newPerson` constructs a new person struct with the given name.
func newPerson(name string) *person {
// Go is a garbage collected language; you can safely
// return a pointer to a local variable - it will only
// be cleaned up by the garbage collector when there
// are no active references to it.
p := person{name: name}
p.age = 42
return &p
}
func main() {
// This syntax creates a new struct.
fmt.Println(person{"Bob", 20})
// You can name the fields when initializing a struct.
fmt.Println(person{name: "Alice", age: 30})
// Omitted fields will be zero-valued.
fmt.Println(person{name: "Fred"})
// An `&` prefix yields a pointer to the struct.
fmt.Println(&person{name: "Ann", age: 40})
// It's idiomatic to encapsulate new struct creation in constructor functions
fmt.Println(newPerson("Jon"))
// Access struct fields with a dot.
s := person{name: "Sean", age: 50}
fmt.Println(s.name)
// Structs are mutable.
s.age = 51
fmt.Println(s)
// You can also use dots with struct pointers - the
// pointers are automatically dereferenced.
sp := &s
sp.age = 52
fmt.Println(sp.age)
// If a struct type is only used for a single value, we don't
// have to give it a name. The value can have an anonymous
// struct type. This technique is commonly used for
// [table-driven tests](testing-and-benchmarking).
dog := struct {
name string
isGood bool
}{
"Rex",
true,
}
fmt.Println(dog)
}
go run structs.go
{Bob 20 1}
{Alice 30 3}
{Fred 0 7}
&{Ann 40 2}
&{Jon 42 1}
Sean
{Sean 51 6}
52
{Rex true}
package intutils
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}
package intutils
import (
"fmt"
"testing"
)
func TestIntMinBasic(t *testing.T) {
ans := IntMin(2, -2)
if ans != -2 {
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
func TestIntMinTableDriven(t *testing.T) {
var tests = []struct {
a, b int
want int
}{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
ans := IntMin(tt.a, tt.b)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
func BenchmarkIntMin(b *testing.B) {
for b.Loop() {
IntMin(1, 2)
}
}
# Setup a module
$ cd ~/cs2613/labs/L19/intutils
$ go mod init example.com/intutils
# Run all tests in the current project in verbose mode.
$ go test -v
== RUN TestIntMinBasic
--- PASS: TestIntMinBasic (0.00s)
=== RUN TestIntMinTableDriven
=== RUN TestIntMinTableDriven/0,1
=== RUN TestIntMinTableDriven/1,0
=== RUN TestIntMinTableDriven/2,-2
=== RUN TestIntMinTableDriven/0,-1
=== RUN TestIntMinTableDriven/-1,0
--- PASS: TestIntMinTableDriven (0.00s)
--- PASS: TestIntMinTableDriven/0,1 (0.00s)
--- PASS: TestIntMinTableDriven/1,0 (0.00s)
--- PASS: TestIntMinTableDriven/2,-2 (0.00s)
--- PASS: TestIntMinTableDriven/0,-1 (0.00s)
--- PASS: TestIntMinTableDriven/-1,0 (0.00s)
PASS
ok examples/testing-and-benchmarking 0.023s
# Run all benchmarks in the current project. All tests
# are run prior to benchmarks. The `bench` flag filters
# benchmark function names with a regexp.
$ go test -bench=.
goos: linux
goarch: amd64
pkg: example.com/main
cpu: 12th Gen Intel(R) Core(TM) i7-12700
BenchmarkIntMin-20 1000000000 0.4362 ns/op
PASS
ok examples.com/main 0.439s
IntMax by copying and modifying IntMin.% go test -cover
PASS
coverage: 50.0% of statements
ok example.com/intutils 0.002s