This commit is contained in:
Geir Okkenhaug Jerstad 2024-08-13 20:31:39 +02:00
commit f7443bbab9
30 changed files with 271 additions and 0 deletions

16
#echo1.go# Normal file
View file

@ -0,0 +1,16 @@
// Echo1 prints its command-line arguments.
package main
import (
"fmt"
"os"
)
func main() {
var s, sep string
for i := 1;i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s)
}

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

9
default.nix Normal file
View file

@ -0,0 +1,9 @@
{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
packages = [
pkgs.go
pkgs.gopls
pkgs.gotools
];
}

8
default.nix~ Normal file
View file

@ -0,0 +1,8 @@
{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
packages = [
pkgs.go
pkgs.gopls
];
}

26
dup/dup1.go Normal file
View file

@ -0,0 +1,26 @@
// Dup1 prints the text of each line that appears more than
// once in the standard input, preceeded by its count.
package main
import(
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
}
//NOTE: ingnoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n , line)
}
}
}

0
dup/dup1.go~ Normal file
View file

33
dup2/dup2.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts)
f.Close()
}
}
}
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}

0
dup2/dup2.go~ Normal file
View file

4
dup2/test.txt Normal file
View file

@ -0,0 +1,4 @@
test
test
test
test

11
dup3/hello Normal file
View file

@ -0,0 +1,11 @@
hello
hello
hello
hello
world
world
world
world
world
hello
hello

27
dup3/main.go Normal file
View file

@ -0,0 +1,27 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
counts := make(map[string]int)
for _, filname := range os.Args[1:] {
data, err := ioutil.ReadFile(filname)
if err != nil {
fmt.Fprint(os.Stderr, "dup3: %v\n", err)
continue
}
for _, line := range strings.Split(string(data), "\n") {
counts[line]++
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}

0
dup3/main.go~ Normal file
View file

BIN
echov1/echo1 Executable file

Binary file not shown.

16
echov1/echo1.go Normal file
View file

@ -0,0 +1,16 @@
//Echo1 prints its command-line arguments.
package main
import (
"fmt"
"os"
)
func main() {
var s, sep string
for i := 1;i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s)
}

BIN
echov2/echo Executable file

Binary file not shown.

19
echov2/echo.go Normal file
View file

@ -0,0 +1,19 @@
// Echov2 prints its command-line arguments.
package main
import (
"fmt"
"os"
)
func main() {
s, sep := "", ""
for _, arg := range os.Args[1:] {
s += sep + arg
sep = " "
}
fmt.Println(s)
for i, v := range os.Args[1:] {
println(i,v)
}
}

0
echov2/echo.go~ Normal file
View file

BIN
echov3/echo Executable file

Binary file not shown.

13
echov3/echo.go Normal file
View file

@ -0,0 +1,13 @@
package main
import (
"fmt"
"os"
"strings"
)
func main() {
fmt.Println(strings.Join(os.Args[1:], " "))
fmt.Print(os.Args[0:])
}

0
echov3/echo.go~ Normal file
View file

BIN
fetch/fetch Executable file

Binary file not shown.

25
fetch/main.go Normal file
View file

@ -0,0 +1,25 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
for _, url := range os.Args[1:] {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Printf("%s", b)
}
}

0
fetch/main.go~ Normal file
View file

7
hello/helloworld.go Normal file
View file

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("hello 世界")
}

7
helloworld.go~ Normal file
View file

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("hello world")
}

BIN
lisajous/hello.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

BIN
lisajous/lisajous Executable file

Binary file not shown.

49
lisajous/lisajous.go Normal file
View file

@ -0,0 +1,49 @@
// Lisajous generates GIF animations of random Lisajous figures.
package main
import (
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
)
var palette = [] color.Color{color.White, color.Black}
const (
whiteIndex = 0
blackindex = 1
)
func main() {
lisajous(os.Stdout)
}
func lisajous(out io.Writer) {
const (
cycles = 5
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blackindex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}

0
lisajous/lisajous.go~ Normal file
View file

BIN
lisajous/out.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB