Compare commits
6 Commits
2a24912a06
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f3c7ef35be | |||
| 5e93134cb2 | |||
| 7765f4c290 | |||
| 228c689243 | |||
| af4de192b3 | |||
| d7c01f2f0e |
@@ -0,0 +1,3 @@
|
|||||||
|
module gitea.zacksolutions.dev/Zakaria/find
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type xkcd struct {
|
||||||
|
Num int `json:"num"`
|
||||||
|
Day string `json:"day"`
|
||||||
|
Month string `json:"month"`
|
||||||
|
Year string `json:"year"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Transcript string `json:"transcript"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Fprintln(os.Stderr, "No file was given")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := os.Args[1]
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Fprintln(os.Stderr, "No search tags were given")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
items []xkcd
|
||||||
|
terms []string
|
||||||
|
input io.ReadCloser
|
||||||
|
cnt int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
// okay so let's decode the file, get search terms and search
|
||||||
|
|
||||||
|
if input, err = os.Open(filename); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Corrupt file: %s\n", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
if err = json.NewDecoder(input).Decode(&items); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Bad json Fromat: %s", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "read %d comics\n", len(items))
|
||||||
|
for _, t := range os.Args[2:] {
|
||||||
|
terms = append(terms, strings.ToLower(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
outer:
|
||||||
|
|
||||||
|
for _, item := range items {
|
||||||
|
title := strings.ToLower(item.Title)
|
||||||
|
transcript := strings.ToLower(item.Transcript)
|
||||||
|
|
||||||
|
for _, term := range terms {
|
||||||
|
if !strings.Contains(title, term) && !strings.Contains(transcript, term) {
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("https://xkcd.com/%d/%s/%s/%s\n", item.Num, item.Day, item.Month, item.Year)
|
||||||
|
cnt++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "found %d comics\n", cnt)
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,3 @@
|
|||||||
|
module gitea.zacksolutions.dev/Zakaria
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getComic(i int) []byte {
|
||||||
|
url := fmt.Sprintf("https://xkcd.com/%d/info.0.json", i)
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Can't reach: %s", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "Skipping %d: got %d\n", i, resp.StatusCode)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "invalid body %s\n", err)
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var (
|
||||||
|
output io.WriteCloser = os.Stdout
|
||||||
|
err error
|
||||||
|
cnt int
|
||||||
|
fails int
|
||||||
|
data []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
output, err = os.Create(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer output.Close()
|
||||||
|
|
||||||
|
}
|
||||||
|
fmt.Fprint(output, "[")
|
||||||
|
defer fmt.Fprint(output, "]")
|
||||||
|
|
||||||
|
for i := 1; fails < 2; i++ {
|
||||||
|
if data = getComic(i); data == nil {
|
||||||
|
fails++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cnt > 0 {
|
||||||
|
fmt.Fprint(output, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := io.Copy(output, bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "STOPPED: %s\n", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
fails = 0
|
||||||
|
cnt++
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "Read %d comics\n", cnt)
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,3 @@
|
|||||||
|
module gitea.zacksolutions.dev/Zakaria/compositions
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Pair struct {
|
||||||
|
Path string
|
||||||
|
Hash string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Pair) String() string {
|
||||||
|
return fmt.Sprintf("Hash of %s is %s", p.Path, p.Hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
type PairWithLength struct {
|
||||||
|
Pair
|
||||||
|
Length int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PairWithLength) String() string {
|
||||||
|
return fmt.Sprintf("Hash of %s is %s; length %d", p.Path, p.Hash, p.Length)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Pair) Filename() string {
|
||||||
|
return filepath.Base(p.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Filenamer interface {
|
||||||
|
Filename() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
p := Pair{"/urs", "0xfdfe"}
|
||||||
|
pl := PairWithLength{Pair{"/usr/bin", "0xdead"}, 13643532}
|
||||||
|
|
||||||
|
var fn Filenamer = PairWithLength{Pair{"/usr/bin", "0xdead"}, 13643532}
|
||||||
|
|
||||||
|
fmt.Println(p)
|
||||||
|
fmt.Println(fn)
|
||||||
|
fmt.Println(pl.Path)
|
||||||
|
fmt.Println(p.Filename())
|
||||||
|
}
|
||||||
@@ -1,12 +1,17 @@
|
|||||||
go 1.27.0
|
go 1.27.1
|
||||||
|
|
||||||
use (
|
use (
|
||||||
./Composite_Types
|
./Composite_Types
|
||||||
./For_If_Switch
|
./For_If_Switch
|
||||||
./Homework_1
|
./Homework_1
|
||||||
|
./Homework_3/cmd/findData
|
||||||
|
./Homework_3/cmd/loadData
|
||||||
./courses
|
./courses
|
||||||
./formatIO
|
./formatIO
|
||||||
./functions
|
./functions
|
||||||
|
./networkingHTTP/cmd/client
|
||||||
|
./networkingHTTP/cmd/server
|
||||||
|
./networkingHTTP/cmd/templ
|
||||||
./pointers
|
./pointers
|
||||||
./strings
|
./strings
|
||||||
./types
|
./types
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module zacksolutions.dev/client
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const url = "https://jsonplaceholder.typicode.com"
|
||||||
|
|
||||||
|
type todo struct {
|
||||||
|
ID int `json:id`
|
||||||
|
Title string `json:title`
|
||||||
|
Status bool `json: status`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const base = "https://jsonplaceholder."
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
resp, err := http.Get(url + "/todos/1/")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
var item todo
|
||||||
|
err = json.Unmarshal(body, &item)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
fmt.Printf("%#v", item)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module zacksolutions.dev
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "Hello! This is from %s\n", r.URL.Path[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
log.Fatal(http.ListenAndServe(":9090", nil))
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module zacksolutions.dev/templ
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type todo struct {
|
||||||
|
UserID int `json:"userId"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var form = `
|
||||||
|
<h2>Todo #{{.ID}}</h2>
|
||||||
|
<div>{{printf "User %d" .UserID}}</div>
|
||||||
|
<div>{{printf "%s (status: %t)" .Title .Completed}}</div>
|
||||||
|
`
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const base = "https://jsonplaceholder.typicode.com/"
|
||||||
|
resp, err := http.Get(base + r.URL.Path[1:])
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var item todo
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tmpl, err := template.New("Mine").Parse(form)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := tmpl.Execute(w, item); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
log.Fatal(http.ListenAndServe(":9090", nil))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user