-
Notifications
You must be signed in to change notification settings - Fork 7
/
bustedGoTutorial.md
68 lines (52 loc) · 966 Bytes
/
bustedGoTutorial.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Follow these instructions to write a `Go` program.
Create a directory to work in:
<!-- @createWorkDir -->
```
workDir=$(mktemp -d --tmpdir mdrip_example_XXXXX)
pushd $workDir
```
Make a file with an `add` function:
<!-- @makeAdder -->
```
cat - <<EOF >add.go
package main
func add(x, y int) (int) { return x + y }
EOF
```
Write a _main program_ to call it:
```
cat - <<EOF >main.go
package main
import "fmt"
func main() {
comment this line to avoid compiler error
fmt.Printf("Calling add on 1 and 2 yields %d.\n", add(1, 2))
}
EOF
```
<!-- @defineGoMod @goCommand -->
```
go mod init myAdder
go mod tidy
echo Dependencies defined.
badecho Enter go build
```
<!-- @compileMain @goCommand -->
```
go build .
```
Now you can run main:
<!-- @runMain -->
```
./myAdder
```
Copy/paste the above into a shell to build and run your program.
Clean up with this command:
```
popd
/bin/rm -rf ${workDir}
```
<!-- @setEnv -->
```
greeting="Hello world!"
```