Showing and Running Code
.code, .play and friends
9 October 2026
A whole file
.code code/hello/main.go shows a file as it is:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
2
Part of a file
An address picks the lines to show. Lines ending in OMIT are left out:
func fib(n int) int {
a, b := 0, 1
for range n {
a, b = b, a+b
}
return a
}
A regular expression alone picks a single line:
3
Highlighting
Lines ending in // HLstep are highlighted when the command ends in HLstep:
func fib(n int) int {
a, b := 0, 1
for range n {
a, b = b, a+b
}
return a
}
Each word marks its own group of lines. This file also has // HLmain:
func main() {
for i := range 10 {
fmt.Print(fib(i), " ")
}
fmt.Println()
}
4
Line numbers and editing
func main() {
for i := range 10 {
fmt.Print(fib(i), " ")
}
fmt.Println()
}
-edit lets the audience change the code on the slide:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
5
Running code locally
The rest of these slides run programs with .play. That needs present running
on your own machine: pages written by present export, such as these examples
on GitHub Pages, only show the code, without a Run button.
To run the programs, start present locally:
go run github.com/some-programs/present@latest serve -doc
Then open http://127.0.0.1:3999 and choose Examples.
6
Running Go
.play runs the program on the machine running present. Click Run, or press shift+enter:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
7
Running only part of the source
Only the selected lines are shown, but the whole file is run:
func main() {
jobs := make(chan int)
results := make(chan string)
for w := range 3 {
go worker(w, jobs, results)
}
go func() {
for i := range 6 {
jobs <- i
}
close(jobs)
}()
for range 6 {
fmt.Println(<-results)
}
}
8
Several files
A txtar archive holds several files; without a go.mod, one is made.
-- go.mod --
module example/multi
go 1.27
-- main.go --
package main
import (
"fmt"
"example/multi/greet"
)
func main() {
fmt.Println(greet.Hello("present"))
}
-- greet/greet.go --
package greet
func Hello(name string) string {
return "Hello from another package, " + name + "!"
}
9
Compile errors
Only main is shown, but the errors' line numbers are those of the file, the one in count too:
func main() {
n := 1
fmt.Println(greeting(), n+"!")
fmt.Println(missing)
}
10
Running Python
A .py file runs with python3:
print(list(itertools.islice(primes(), 20)))
11
Running JavaScript and TypeScript
.js files run with node, .ts files too once tsc has checked and compiled them:
for (let i = 1; i <= 15; i++) {
console.log((i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i);
}
function area(s: Shape): number {
switch (s.kind) {
case 'circle':
return Math.PI * s.radius ** 2;
case 'square':
return s.side ** 2;
}
}
12
Other languages
A program starting with #! runs with that interpreter:
#!/bin/sh
# Programs starting with #! run with that interpreter instead of Go.
echo "Running on $(uname -s) as $(whoami)"
date
13
Source of slide 1S closes
examples/code.slide
1 # Showing and Running Code
2 .code, .play and friends
3 9 Oct 2026
4
5 Ada Example
6 ada@example.com
Source of slide 2S closes
examples/code.slide
8 ## A whole file
9
10 `.code code/hello/main.go` shows a file as it is:
11
12 .code code/hello/main.go
File used
code/hello/main.go
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Println("Hello, 世界")
7 }
Source of slide 3S closes
examples/code.slide
14 ## Part of a file
15
16 An address picks the lines to show. Lines ending in `OMIT` are left out:
17
18 .code code/fib/main.go /START OMIT/,/END OMIT/
19
20 A regular expression alone picks a single line:
21
22 .code code/fib/main.go /^func main/
File used
code/fib/main.go
1 package main
2
3 import "fmt"
4
5 // START OMIT
6 func fib(n int) int {
7 a, b := 0, 1
8 for range n {
9 a, b = b, a+b // HLstep
10 }
11 return a
12 }
13
14 // END OMIT
15
16 func main() {
17 for i := range 10 {
18 fmt.Print(fib(i), " ") // HLmain
19 }
20 fmt.Println()
21 }
Source of slide 4S closes
examples/code.slide
24 ## Highlighting
25
26 Lines ending in `// HLstep` are highlighted when the command ends in `HLstep`:
27
28 .code code/fib/main.go /^func fib/,/^}/ HLstep
29
30 Each word marks its own group of lines. This file also has `// HLmain`:
31
32 .code code/fib/main.go /^func main/,/^}/ HLmain
File used
code/fib/main.go
1 package main
2
3 import "fmt"
4
5 // START OMIT
6 func fib(n int) int {
7 a, b := 0, 1
8 for range n {
9 a, b = b, a+b // HLstep
10 }
11 return a
12 }
13
14 // END OMIT
15
16 func main() {
17 for i := range 10 {
18 fmt.Print(fib(i), " ") // HLmain
19 }
20 fmt.Println()
21 }
Source of slide 5S closes
examples/code.slide
34 ## Line numbers and editing
35
36 .code -numbers code/fib/main.go /^func main/,/^}/
37
38 `-edit` lets the audience change the code on the slide:
39
40 .code -edit code/hello/main.go
Files used
code/fib/main.go
1 package main
2
3 import "fmt"
4
5 // START OMIT
6 func fib(n int) int {
7 a, b := 0, 1
8 for range n {
9 a, b = b, a+b // HLstep
10 }
11 return a
12 }
13
14 // END OMIT
15
16 func main() {
17 for i := range 10 {
18 fmt.Print(fib(i), " ") // HLmain
19 }
20 fmt.Println()
21 }
code/hello/main.go
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Println("Hello, 世界")
7 }
Source of slide 6S closes
examples/code.slide
42 ## Running code locally
43
44 The rest of these slides run programs with `.play`. That needs present running
45 on your own machine: pages written by `present export`, such as these examples
46 on GitHub Pages, only show the code, without a Run button.
47
48 To run the programs, start present locally:
49
50 go run github.com/some-programs/present@latest serve -doc
51
52 Then open http://127.0.0.1:3999 and choose Examples.
Source of slide 7S closes
examples/code.slide
54 ## Running Go
55
56 `.play` runs the program on the machine running present. Click Run, or press shift+enter:
57
58 .play code/hello/main.go
59
60 : Run present serve -play=false to turn off running programs.
File used
code/hello/main.go
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Println("Hello, 世界")
7 }
Source of slide 8S closes
examples/code.slide
62 ## Running only part of the source
63
64 Only the selected lines are shown, but the whole file is run:
65
66 .play -edit code/pipeline/main.go /^func main/,/^}/
File used
code/pipeline/main.go
1 package main
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 func main() {
9 jobs := make(chan int)
10 results := make(chan string)
11
12 for w := range 3 {
13 go worker(w, jobs, results)
14 }
15
16 go func() {
17 for i := range 6 {
18 jobs <- i
19 }
20 close(jobs)
21 }()
22
23 for range 6 {
24 fmt.Println(<-results)
25 }
26 }
27
28 func worker(id int, jobs <-chan int, results chan<- string) {
29 for j := range jobs {
30 time.Sleep(200 * time.Millisecond)
31 results <- fmt.Sprintf("worker %d did job %d", id, j)
32 }
33 }
Source of slide 9S closes
examples/code.slide
68 ## Several files
69
70 A [txtar](https://pkg.go.dev/golang.org/x/tools/txtar) archive holds several files; without a go.mod, one is made.
71
72 .fontsize 0.9
73
74 .play code/multi.txtar
File used
code/multi.txtar
1 -- go.mod --
2 module example/multi
3 go 1.27
4 -- main.go --
5 package main
6
7 import (
8 "fmt"
9
10 "example/multi/greet"
11 )
12
13 func main() {
14 fmt.Println(greet.Hello("present"))
15 }
16 -- greet/greet.go --
17 package greet
18
19 func Hello(name string) string {
20 return "Hello from another package, " + name + "!"
21 }
Source of slide 10S closes
examples/code.slide
76 ## Compile errors
77
78 Only `main` is shown, but the errors' line numbers are those of the file, the one in `count` too:
79
80 .play -numbers code/_broken/main.go /^func main/,/^}/
File used
code/_broken/main.go
1 // This program does not compile, to show that the line numbers in the
2 // compiler's errors match the ones on the slide. It is in a directory
3 // starting with _ so that the go command leaves it out of ./...
4 package main
5
6 import "fmt"
7
8 func greeting() string {
9 return "hello"
10 }
11
12 func main() {
13 n := 1
14 fmt.Println(greeting(), n+"!")
15 fmt.Println(missing)
16 }
17
18 func count() int {
19 return "not a number"
20 }
Source of slide 11S closes
examples/code.slide
82 ## Running Python
83
84 A `.py` file runs with `python3`:
85
86 .play -edit code/primes.py /^print/
File used
code/primes.py
1 import itertools
2
3
4 def primes():
5 """Yields the prime numbers, using the sieve of Eratosthenes."""
6 composites = {}
7 for n in itertools.count(2):
8 if n not in composites:
9 yield n
10 composites[n * n] = [n]
11 continue
12 for p in composites.pop(n):
13 composites.setdefault(n + p, []).append(p)
14
15
16 print(list(itertools.islice(primes(), 20)))
Source of slide 12S closes
examples/code.slide
88 ## Running JavaScript and TypeScript
89
90 `.js` files run with `node`, `.ts` files too once `tsc` has checked and compiled them:
91
92 .play code/fizzbuzz.js
93
94 .play code/shapes.ts /^function area/,/^}/
Files used
code/fizzbuzz.js
1 for (let i = 1; i <= 15; i++) {
2 console.log((i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i);
3 }
code/shapes.ts
1 type Shape =
2 | { kind: 'circle'; radius: number }
3 | { kind: 'square'; side: number };
4
5 function area(s: Shape): number {
6 switch (s.kind) {
7 case 'circle':
8 return Math.PI * s.radius ** 2;
9 case 'square':
10 return s.side ** 2;
11 }
12 }
13
14 const shapes: Shape[] = [
15 { kind: 'circle', radius: 1 },
16 { kind: 'square', side: 2 },
17 ];
18 for (const s of shapes) {
19 console.log(s.kind, area(s).toFixed(2));
20 }
Source of slide 13S closes
examples/code.slide
96 ## Other languages
97
98 A program starting with `#!` runs with that interpreter:
99
100 .play code/system.sh
File used
code/system.sh
1 #!/bin/sh
2 # Programs starting with #! run with that interpreter instead of Go.
3 echo "Running on $(uname -s) as $(whoami)"
4 date
No sourceS closes
present adds this slide itself; it has no source.
Use the left and right arrow keys or click the left and right
edges of the page to navigate between slides.
Press 'S' to show or hide the source of the slide.
(Press 'H', navigate or click this message to hide it.)