├── 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 | 6 | 7 | 8 |

The name is Alexander

9 |

What's the last name? It's Kallaway

10 | 11 | -------------------------------------------------------------------------------- /web/003-html-from-text-template/template.gohtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

The name is Alexander

9 |

What's the last name? It's Kallaway

10 | 11 | -------------------------------------------------------------------------------- /web/005-template-data/template.gohtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {{ $values := . }} 9 |

What are the values passed in?

10 |

{{$values}}

11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # golang-snippets 2 | Bits, pieces and examples of Golang code 3 | 4 | I am posting them here as I go, most of them can be run from their respective little folders. 5 | 6 | Please let me know here or on Twitter [@ka11away](https://twitter.com/ka11away) if you'd like more example on specific topics. 7 | For example: structs, interfaces, variables, types, etc. :) 8 | -------------------------------------------------------------------------------- /web/005-template-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 | func main() { 16 | nums := []int{32, 14, 34} 17 | err := tpl.ExecuteTemplate(os.Stdout, "template.gohtml", nums) 18 | if err != nil { 19 | log.Fatal("Error executing template:", err) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /web/007-template-func/template.gohtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Go Template 6 | 7 | 8 |

Some facts about College

9 | 10 |

College Name: {{ uc .Name }}

11 | 12 |

What were the subjects taught there? Tell me the first three that come to mind.

13 | 14 |

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 | 6 | 7 | 8 |

What do we need to do today?

9 | 14 | 15 |

Can you make a numbered list of that?

16 | 21 | 22 |

Tell me about countries and their languages

23 | 28 | 29 |

Tell me about your pet!

30 |

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 | 30 | 31 | 32 |

The name is ` + fname + `

33 |

What's the last name? It's ` + lname + `

34 | 35 | `) 36 | 37 | nf, err := os.Create("index.html") 38 | 39 | if err != nil { 40 | log.Fatal("Error printing the file:", err) 41 | } 42 | 43 | defer nf.Close() 44 | 45 | io.Copy(nf, strings.NewReader(template)) 46 | } 47 | -------------------------------------------------------------------------------- /web/004-multi-template/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "html/template" 6 | "log" 7 | "os" 8 | ) 9 | 10 | // When you run this file, it takes multiple templates and loads them at once. 11 | // Then we manually choose which one to print out 12 | // Parsing the templates in init() enables us 13 | // to only parse them once and improves efficiency 14 | var tpl *template.Template 15 | 16 | func init() { 17 | // Must() will panic automatically if it gets non-nil error 18 | tpl = template.Must(template.ParseGlob("templates/*.gohtml")) 19 | } 20 | 21 | func main() { 22 | err := tpl.Execute(os.Stdout, nil) 23 | if err != nil { 24 | log.Fatal("Error printing first template to stdout:", err) 25 | } 26 | 27 | fmt.Println() 28 | 29 | err = tpl.ExecuteTemplate(os.Stdout, "second.gohtml", nil) 30 | if err != nil { 31 | log.Fatal("Error printing second template to stdout:", err) 32 | } 33 | 34 | fmt.Println() 35 | 36 | err = tpl.ExecuteTemplate(os.Stdout, "third.gohtml", nil) 37 | if err != nil { 38 | log.Fatal("Error printing third template to stdout:", err) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /web/007-template-func/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "os" 7 | "strings" 8 | ) 9 | 10 | var tpl *template.Template 11 | 12 | var funcMap = template.FuncMap{ 13 | "uc": strings.ToUpper, 14 | "lc": strings.ToLower, 15 | "limit3": displayFirstThree, 16 | } 17 | 18 | type college struct { 19 | Subjects []string 20 | Name string 21 | } 22 | 23 | func init() { 24 | tpl = template.Must(template.New("").Funcs(funcMap).ParseFiles("template.gohtml")) 25 | } 26 | 27 | func displayFirstThree(items []string) string { 28 | itemsToShow := items[:3] 29 | listString := strings.Join(itemsToShow, ", ") 30 | 31 | return listString 32 | } 33 | 34 | func main() { 35 | 36 | studiedSubjects := []string{ 37 | "Calculus", 38 | "English", 39 | "Physics", 40 | "Biology", 41 | "Economics", 42 | "Business Strategy", 43 | } 44 | 45 | collegeName := "Imaginary College of Mathematics & Technology" 46 | 47 | exampleCollege := college{ 48 | studiedSubjects, 49 | collegeName, 50 | } 51 | 52 | err := tpl.ExecuteTemplate(os.Stdout, "template.gohtml", exampleCollege) 53 | if err != nil { 54 | log.Fatalln("Error executing the template:", err) 55 | } 56 | } 57 | --------------------------------------------------------------------------------