├── web ├── 004-multi-template │ ├── templates │ │ ├── second.gohtml │ │ ├── third.gohtml │ │ └── first.gohtml │ └── main.go ├── 002-simple-template │ ├── index.html │ └── main.go ├── 003-html-from-text-template │ ├── template.gohtml │ └── main.go ├── 005-template-data │ ├── template.gohtml │ └── main.go ├── 007-template-func │ ├── template.gohtml │ └── main.go ├── 001-hello-server │ └── main.go └── 006-template-composite-data │ ├── template.gohtml │ └── main.go ├── golang-intro-presentation └── Golang Intro by Alex Kallaway.pdf ├── README.md ├── scripts └── save-image │ └── main.go └── interfaces └── shape └── main.go /web/004-multi-template/templates/second.gohtml: -------------------------------------------------------------------------------- 1 | ******* 2 | SECOND TEMPLATE 3 | ******* -------------------------------------------------------------------------------- /web/004-multi-template/templates/third.gohtml: -------------------------------------------------------------------------------- 1 | @@@@@@@ 2 | THIRD TEMPLATE MARKING THE END 3 | @@@@@@@ -------------------------------------------------------------------------------- /web/004-multi-template/templates/first.gohtml: -------------------------------------------------------------------------------- 1 | $$$$$$$ 2 | FIRST TEMPLATE MARKING THE BEGINNING 3 | $$$$$$$ -------------------------------------------------------------------------------- /golang-intro-presentation/Golang Intro by Alex Kallaway.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kallaway/golang-snippets/HEAD/golang-intro-presentation/Golang Intro by Alex Kallaway.pdf -------------------------------------------------------------------------------- /web/002-simple-template/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |College Name: {{ uc .Name }}
11 | 12 |Let me think... The subjects were: {{ limit3 .Subjects }}
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /scripts/save-image/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "log" 6 | "net/http" 7 | "os" 8 | ) 9 | 10 | func main() { 11 | resp, err := http.Get("https://picsum.photos/200/300/") 12 | 13 | if err != nil { 14 | log.Fatal("Error:", err) 15 | } 16 | 17 | defer resp.Body.Close() 18 | 19 | file, err := os.Create("img/image1.jpg") 20 | if err != nil { 21 | log.Fatal(err) 22 | } 23 | 24 | _, err = io.Copy(file, resp.Body) 25 | 26 | if err != nil { 27 | log.Fatal(err) 28 | } 29 | 30 | file.Close() 31 | } 32 | -------------------------------------------------------------------------------- /web/003-html-from-text-template/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "os" 7 | ) 8 | 9 | // run this file to generate an index.html from the go template 10 | func main() { 11 | tpl, err := template.ParseFiles("template.gohtml") 12 | if err != nil { 13 | log.Fatal("Error parsing the template", err) 14 | } 15 | 16 | nf, err := os.Create("index.html") 17 | if err != nil { 18 | log.Fatal("Error creating a file:", err) 19 | } 20 | 21 | defer nf.Close() 22 | 23 | err = tpl.Execute(nf, nil) 24 | if err != nil { 25 | log.Fatal("Error executing the template:", err) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /web/001-hello-server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Run this file, then go to localhost:8080. 4 | // Type in localhost:8080/YourName 5 | // You will see the server print out "Hello YourName" on the page 6 | 7 | import ( 8 | "net/http" 9 | "strings" 10 | ) 11 | 12 | func sayHello(w http.ResponseWriter, r *http.Request) { 13 | m := r.URL.Path 14 | m = strings.TrimPrefix(m, "/") 15 | m = "Hello " + m 16 | 17 | // convert to a slice of bytes for the Writer 18 | w.Write([]byte(m)) 19 | } 20 | 21 | func main() { 22 | http.HandleFunc("/", sayHello) 23 | 24 | if err := http.ListenAndServe(":8080", nil); err != nil { 25 | panic(err) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /interfaces/shape/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type triangle struct { 6 | height float64 7 | base float64 8 | } 9 | 10 | type square struct { 11 | sideLength float64 12 | } 13 | 14 | type shape interface { 15 | getArea() float64 16 | } 17 | 18 | func main() { 19 | tr := triangle{ 20 | height: 23, 21 | base: 46, 22 | } 23 | sq := square{ 24 | sideLength: 44, 25 | } 26 | 27 | printArea(tr) 28 | printArea(sq) 29 | } 30 | 31 | func (s square) getArea() float64 { 32 | return s.sideLength * s.sideLength 33 | } 34 | 35 | func (t triangle) getArea() float64 { 36 | return 0.5 * t.base * t.height 37 | } 38 | 39 | func printArea(s shape) { 40 | fmt.Println(s.getArea()) 41 | } 42 | -------------------------------------------------------------------------------- /web/006-template-composite-data/template.gohtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |I have a {{.Animal.Kind}} named {{.Animal.Name}}, {{.Animal.Age}} years old.
31 | 32 | -------------------------------------------------------------------------------- /web/006-template-composite-data/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "os" 7 | ) 8 | 9 | var tpl *template.Template 10 | 11 | func init() { 12 | tpl = template.Must(template.ParseFiles("template.gohtml")) 13 | } 14 | 15 | type animal struct { 16 | Name string 17 | Kind string 18 | Age int 19 | } 20 | 21 | type templateData struct { 22 | Td []string 23 | Lang map[string]string 24 | Animal animal 25 | } 26 | 27 | func main() { 28 | todos := []string{"write", "meditate", "code", "read"} 29 | languages := map[string]string{ 30 | "Japan": "Japanese", 31 | "England": "English", 32 | "Mexico": "Spanish", 33 | "China": "Chinese", 34 | } 35 | oreo := animal{ 36 | Name: "Oreo", 37 | Kind: "Dog", 38 | Age: 4, 39 | } 40 | 41 | tplData := templateData{ 42 | Td: todos, 43 | Lang: languages, 44 | Animal: oreo, 45 | } 46 | 47 | err := tpl.ExecuteTemplate(os.Stdout, "template.gohtml", tplData) 48 | 49 | if err != nil { 50 | log.Fatal("Error executing template:", err) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /web/002-simple-template/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | // expects 2 command line arguments 12 | // First name and Last name 13 | 14 | // It creates an index.html file with 15 | // content filled in based on the arguments given 16 | func main() { 17 | if len(os.Args) != 3 { 18 | log.Fatal(`Error: You haven't provided enough arguments. 19 | This program expects 2 command line arguments: first name and last name.`) 20 | } 21 | 22 | fname := os.Args[1] 23 | lname := os.Args[2] 24 | 25 | template := fmt.Sprint(` 26 | 27 | 28 | 29 |