From 2c8e47ffadbc081355b01a4e287123f012022840 Mon Sep 17 00:00:00 2001 From: Todd McLeod Date: Thu, 18 May 2023 22:45:21 -0400 Subject: [PATCH] persistently, patiently, you are bound to succeed --- 000-aq-json-01/main.go | 24 ++++++++++++++++++ 000-aq-json-02/main.go | 26 +++++++++++++++++++ 000-aq-json-03-decode/main.go | 37 +++++++++++++++++++++++++++ 000-aq-json-04-unmarshal/main.go | 42 +++++++++++++++++++++++++++++++ 000-aq-json-05-tags/main.go | 37 +++++++++++++++++++++++++++ 000-ar-checksum-01/main.go | 34 +++++++++++++++++++++++++ 000-ar-checksum-01/simpleFile.txt | 1 + 7 files changed, 201 insertions(+) create mode 100644 000-aq-json-01/main.go create mode 100644 000-aq-json-02/main.go create mode 100644 000-aq-json-03-decode/main.go create mode 100644 000-aq-json-04-unmarshal/main.go create mode 100644 000-aq-json-05-tags/main.go create mode 100644 000-ar-checksum-01/main.go create mode 100644 000-ar-checksum-01/simpleFile.txt diff --git a/000-aq-json-01/main.go b/000-aq-json-01/main.go new file mode 100644 index 0000000..4288877 --- /dev/null +++ b/000-aq-json-01/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +// curl -i https://api.github.com/users/GoesToEleven + +func main() { + resp, err := http.Get("https://api.github.com/users/GoesToEleven") + if err != nil { + log.Fatalf("error getting request: %s", err) + } + defer resp.Body.Close() + + ct := resp.Header.Get("Content-Type") + fmt.Println(ct) + + fmt.Println(resp.Header.Get("server")) + fmt.Println(resp.Header.Get("date")) + fmt.Printf("%#v", resp.Header.Get("content-length")) +} \ No newline at end of file diff --git a/000-aq-json-02/main.go b/000-aq-json-02/main.go new file mode 100644 index 0000000..4fb4207 --- /dev/null +++ b/000-aq-json-02/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "io" + "log" + "net/http" + "os" +) + +// curl -i -H 'User-Agent: go' https://api.github.com/users/GoesToEleven + +func main() { + resp, err := http.Get("https://api.github.com/users/GoesToEleven") + if err != nil { + log.Fatalf("error getting request: %s", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Fatalf("error: %s", resp.Status) + } + + if _, err := io.Copy(os.Stdout, resp.Body); err != nil { + log.Fatalf("error - unable to copy: %s", err) + } +} diff --git a/000-aq-json-03-decode/main.go b/000-aq-json-03-decode/main.go new file mode 100644 index 0000000..b37bf54 --- /dev/null +++ b/000-aq-json-03-decode/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +// serialization: json, xml, csv, protobuffers + +func main() { + resp, err := http.Get("https://api.github.com/users/GoesToEleven") + if err != nil { + log.Fatalf("error getting request: %s", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Fatalf("error: %s", resp.Status) + } + + var u User + dec := json.NewDecoder(resp.Body) + if err := dec.Decode(&u); err != nil { + log.Fatalf("error decoding: %s", err) + } + fmt.Println("Name\t\t", u.Name) + fmt.Println("Public Repos\t", u.Public_Repos) + fmt.Println("----") + fmt.Printf("%#v", u) +} + +type User struct { + Name string + Public_Repos int +} diff --git a/000-aq-json-04-unmarshal/main.go b/000-aq-json-04-unmarshal/main.go new file mode 100644 index 0000000..aaefca0 --- /dev/null +++ b/000-aq-json-04-unmarshal/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" +) + +// serialization: json, xml, csv, protobuffers + +func main() { + resp, err := http.Get("https://api.github.com/users/GoesToEleven") + if err != nil { + log.Fatalf("error getting request: %s", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Fatalf("error: %s", resp.Status) + } + + var u User + xb, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalf("error reading: %s", err) + } + if err := json.Unmarshal(xb, &u); err != nil { + log.Fatalf("unmarshal error: %s", err) + } + + fmt.Println("Name\t\t", u.Name) + fmt.Println("Public Repos\t", u.Public_Repos) + fmt.Println("----") + fmt.Printf("%#v", u) +} + +type User struct { + Name string + Public_Repos int +} diff --git a/000-aq-json-05-tags/main.go b/000-aq-json-05-tags/main.go new file mode 100644 index 0000000..0a403cb --- /dev/null +++ b/000-aq-json-05-tags/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +// command palette (ctrl+shift+p): Go: add tags to struct fields + +func main() { + resp, err := http.Get("https://api.github.com/users/GoesToEleven") + if err != nil { + log.Fatalf("error getting request: %s", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Fatalf("error: %s", resp.Status) + } + + var u User + dec := json.NewDecoder(resp.Body) + if err := dec.Decode(&u); err != nil { + log.Fatalf("error decoding: %s", err) + } + fmt.Println("Name\t\t", u.Name) + fmt.Println("Public Repos\t", u.NumRepos) + fmt.Println("----") + fmt.Printf("%#v", u) +} + +type User struct { + Name string `json:"name,omitempty"` + NumRepos int `json:"num_repos,omitempty"` +} diff --git a/000-ar-checksum-01/main.go b/000-ar-checksum-01/main.go new file mode 100644 index 0000000..1b17663 --- /dev/null +++ b/000-ar-checksum-01/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "crypto/sha1" + "fmt" + "io" + "log" + "os" +) + +func main() { + s, err := sha1Sum("./simpleFile.txt") + if err != nil { + log.Fatalf("error getting sha1: %s", err) + } + fmt.Println(s) +} + +func sha1Sum(fileName string) (string, error) { + file, err := os.Open(fileName) + if err != nil { + return "", nil + } + defer file.Close() + + w := sha1.New() + if _, err := io.Copy(w, file); err != nil { + return "", err + } + sig := w.Sum(nil) + return fmt.Sprintf("%x", sig), nil +} + +// cat simpleFile.txt | sha1sum diff --git a/000-ar-checksum-01/simpleFile.txt b/000-ar-checksum-01/simpleFile.txt new file mode 100644 index 0000000..fdaccda --- /dev/null +++ b/000-ar-checksum-01/simpleFile.txt @@ -0,0 +1 @@ +this is a simple text file \ No newline at end of file