-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
persistently, patiently, you are bound to succeed
- Loading branch information
1 parent
f6aad34
commit 2c8e47f
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is a simple text file |