Skip to content

Commit

Permalink
persistently, patiently, you are bound to succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
GoesToEleven committed May 19, 2023
1 parent f6aad34 commit 2c8e47f
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 000-aq-json-01/main.go
Original file line number Diff line number Diff line change
@@ -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"))
}
26 changes: 26 additions & 0 deletions 000-aq-json-02/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions 000-aq-json-03-decode/main.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions 000-aq-json-04-unmarshal/main.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions 000-aq-json-05-tags/main.go
Original file line number Diff line number Diff line change
@@ -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"`
}
34 changes: 34 additions & 0 deletions 000-ar-checksum-01/main.go
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions 000-ar-checksum-01/simpleFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a simple text file

0 comments on commit 2c8e47f

Please sign in to comment.