commit f7443bbab911d33df7d264ff03c5bf614dbc1644 Author: Geir Okkenhaug Jerstad Date: Tue Aug 13 20:31:39 2024 +0200 init diff --git a/#echo1.go# b/#echo1.go# new file mode 100644 index 0000000..88235c8 --- /dev/null +++ b/#echo1.go# @@ -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) +} diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..9a9a550 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..1a7e270 --- /dev/null +++ b/default.nix @@ -0,0 +1,9 @@ +{ pkgs ? import {}}: + +pkgs.mkShell { + packages = [ + pkgs.go + pkgs.gopls + pkgs.gotools + ]; +} diff --git a/default.nix~ b/default.nix~ new file mode 100644 index 0000000..2ef9784 --- /dev/null +++ b/default.nix~ @@ -0,0 +1,8 @@ +{ pkgs ? import {}}: + +pkgs.mkShell { + packages = [ + pkgs.go + pkgs.gopls + ]; +} diff --git a/dup/dup1.go b/dup/dup1.go new file mode 100644 index 0000000..fc9e3f3 --- /dev/null +++ b/dup/dup1.go @@ -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) + } + } + +} diff --git a/dup/dup1.go~ b/dup/dup1.go~ new file mode 100644 index 0000000..e69de29 diff --git a/dup2/dup2.go b/dup2/dup2.go new file mode 100644 index 0000000..6fdb424 --- /dev/null +++ b/dup2/dup2.go @@ -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()]++ + } +} diff --git a/dup2/dup2.go~ b/dup2/dup2.go~ new file mode 100644 index 0000000..e69de29 diff --git a/dup2/test.txt b/dup2/test.txt new file mode 100644 index 0000000..d0c7fbe --- /dev/null +++ b/dup2/test.txt @@ -0,0 +1,4 @@ +test +test +test +test diff --git a/dup3/hello b/dup3/hello new file mode 100644 index 0000000..74619c5 --- /dev/null +++ b/dup3/hello @@ -0,0 +1,11 @@ +hello +hello +hello +hello +world +world +world +world +world +hello +hello diff --git a/dup3/main.go b/dup3/main.go new file mode 100644 index 0000000..08e8397 --- /dev/null +++ b/dup3/main.go @@ -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) + } + } +} diff --git a/dup3/main.go~ b/dup3/main.go~ new file mode 100644 index 0000000..e69de29 diff --git a/echov1/echo1 b/echov1/echo1 new file mode 100755 index 0000000..d5b9550 Binary files /dev/null and b/echov1/echo1 differ diff --git a/echov1/echo1.go b/echov1/echo1.go new file mode 100644 index 0000000..b686f00 --- /dev/null +++ b/echov1/echo1.go @@ -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) +} diff --git a/echov2/echo b/echov2/echo new file mode 100755 index 0000000..b4f807f Binary files /dev/null and b/echov2/echo differ diff --git a/echov2/echo.go b/echov2/echo.go new file mode 100644 index 0000000..1c94098 --- /dev/null +++ b/echov2/echo.go @@ -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) + } +} diff --git a/echov2/echo.go~ b/echov2/echo.go~ new file mode 100644 index 0000000..e69de29 diff --git a/echov3/echo b/echov3/echo new file mode 100755 index 0000000..70decb0 Binary files /dev/null and b/echov3/echo differ diff --git a/echov3/echo.go b/echov3/echo.go new file mode 100644 index 0000000..ec444f3 --- /dev/null +++ b/echov3/echo.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +func main() { + fmt.Println(strings.Join(os.Args[1:], " ")) + fmt.Print(os.Args[0:]) + +} diff --git a/echov3/echo.go~ b/echov3/echo.go~ new file mode 100644 index 0000000..e69de29 diff --git a/fetch/fetch b/fetch/fetch new file mode 100755 index 0000000..937058a Binary files /dev/null and b/fetch/fetch differ diff --git a/fetch/main.go b/fetch/main.go new file mode 100644 index 0000000..4136c33 --- /dev/null +++ b/fetch/main.go @@ -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) + } +} diff --git a/fetch/main.go~ b/fetch/main.go~ new file mode 100644 index 0000000..e69de29 diff --git a/hello/helloworld.go b/hello/helloworld.go new file mode 100644 index 0000000..69b53fa --- /dev/null +++ b/hello/helloworld.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello 世界") +} diff --git a/helloworld.go~ b/helloworld.go~ new file mode 100644 index 0000000..c048119 --- /dev/null +++ b/helloworld.go~ @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world") +} diff --git a/lisajous/hello.gif b/lisajous/hello.gif new file mode 100644 index 0000000..16a1b0b Binary files /dev/null and b/lisajous/hello.gif differ diff --git a/lisajous/lisajous b/lisajous/lisajous new file mode 100755 index 0000000..4b59625 Binary files /dev/null and b/lisajous/lisajous differ diff --git a/lisajous/lisajous.go b/lisajous/lisajous.go new file mode 100644 index 0000000..3f9ff9c --- /dev/null +++ b/lisajous/lisajous.go @@ -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) +} diff --git a/lisajous/lisajous.go~ b/lisajous/lisajous.go~ new file mode 100644 index 0000000..e69de29 diff --git a/lisajous/out.gif b/lisajous/out.gif new file mode 100644 index 0000000..45fb469 Binary files /dev/null and b/lisajous/out.gif differ