repo stringlengths 6 47 | file_url stringlengths 77 269 | file_path stringlengths 5 186 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-07 08:35:43 2026-01-07 08:55:24 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.5.2/main.go | de/code/src/apps/ch.5.2/main.go | // Example code for Chapter 5.2 from "Build Web Application with Golang"
// Purpose: Use SQL driver to perform simple CRUD operations.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
const (
DB_USER = "user"
DB_PASSWORD = ""
DB_NAME = "test"
)
func main() {
dbSouce := f... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/main.go | de/code/src/apps/ch.2.3/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Goes over if, else, switch conditions, loops and defer.
package main
import "fmt"
func computedValue() int {
return 1
}
func show_if() {
fmt.Println("\n#show_if()")
x := computedValue()
integer := 23
fmt.Println("x =", x)
fmt.... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/import_packages/main.go | de/code/src/apps/ch.2.3/import_packages/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows different ways of importing a package.
// Note: For the package `only_call_init`, we reference the path from the
// base directory of `$GOPATH/src`. The reason being Golang discourage
// the use of relative paths when import pack... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/import_packages/only_call_init/only_call_init.go | de/code/src/apps/ch.2.3/import_packages/only_call_init/only_call_init.go | package only_call_init
import "fmt"
func init() {
fmt.Println("only_call_init.init() was called.")
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/type_function/main.go | de/code/src/apps/ch.2.3/type_function/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows how to define a function type
package main
import "fmt"
type testInt func(int) bool // define a function type of variable
func isOdd(integer int) bool {
if integer%2 == 0 {
return false
}
return true
}
func isEven(intege... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/variadic_functions/main.go | de/code/src/apps/ch.2.3/variadic_functions/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows how to return multiple values from a function
package main
import "fmt"
// return results of A + B and A * B
func SumAndProduct(A, B int) (int, int) {
return A + B, A * B
}
func main() {
x := 3
y := 4
xPLUSy, xTIMESy := S... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/hidden_print_methods/main.go | de/code/src/apps/ch.2.3/hidden_print_methods/main.go | // As of Google go 1.1.2, `println()` and `print()` are hidden functions included from the runtime package.
// However it's encouraged to use the print functions from the `fmt` package.
package main
import "fmt"
func f() {
fmt.Println("First")
print("Second ")
println(" Third")
}
func main() {
f()
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/panic_and_recover/main.go | de/code/src/apps/ch.2.3/panic_and_recover/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Showing how to use `panic()` and `recover()`
package main
import (
"fmt"
"os"
)
var user = os.Getenv("USER")
func check_user() {
if user == "" {
panic("no value for $USER")
}
fmt.Println("Environment Variable `USER` =", user)... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/basic_functions/main.go | de/code/src/apps/ch.2.3/basic_functions/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Creating a basic function
package main
import "fmt"
// return greater value between a and b
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
x := 3
y := 4
z := 5
max_xy := max(x, y) // call function ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go | de/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows passing a variable by value and reference
package main
import "fmt"
func add_by_value(a int) int {
a = a + 1
return a
}
func add_by_reference(a *int) int {
*a = *a + 1
return *a
}
func show_add_by_value() {
x := 3
fmt... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.4.3/main.go | de/code/src/apps/ch.4.3/main.go | // Example code for Chapter 4.3 from "Build Web Application with Golang"
// Purpose: Shows how to properly escape input
package main
import (
"html/template"
"net/http"
textTemplate "text/template"
)
var t *template.Template = template.Must(template.ParseFiles("index.gtpl"))
func index(w http.ResponseWriter, r *h... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.1.2/main.go | de/code/src/apps/ch.1.2/main.go | // Example code for Chapter 1.2 from "Build Web Application with Golang"
// Purpose: Run this file to check if your workspace is setup correctly.
// To run, navigate to the current directory in a console and type `go run main.go`
// If the text "Hello World" isn't shown, then setup your workspace again.
package main
i... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/select_channel/main.go | de/code/src/apps/ch.2.7/select_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to use `select`
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 1, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/buffered_channel/main.go | de/code/src/apps/ch.2.7/buffered_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to use a buffered channel
package main
import "fmt"
func main() {
c := make(chan int, 2) // change 2 to 1 will have runtime error, but 3 is fine
c <- 1
c <- 2
fmt.Println(<-c)
fmt.Println(<-c)
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/goroutine/main.go | de/code/src/apps/ch.2.7/goroutine/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to launch a simple gorountine
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world") // create a new goroutine
say... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/range_and_close_channel/main.go | de/code/src/apps/ch.2.7/range_and_close_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to close and interate through a channel
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 1, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/unbuffered_channel/main.go | de/code/src/apps/ch.2.7/unbuffered_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to create and use a unbuffered channel
package main
import "fmt"
func sum(a []int, c chan int) {
total := 0
for _, v := range a {
total += v
}
c <- total // send total to c
}
func main() {
a := []int{7, 2, 8, -9, 4,... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.7/timeout/main.go | de/code/src/apps/ch.2.7/timeout/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to create and use a timeout
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v := <-c:
fmt.Println(v)
case <-time.After(5 * tim... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.3.4/main.go | de/code/src/apps/ch.3.4/main.go | // Example code for Chapter 3.4 from "Build Web Application with Golang"
// Purpose: Shows how to create a handler for `http.ListenAndServe()`
// Run `go run main.go` then access `http://localhost:9090`
package main
import (
"fmt"
"net/http"
)
type MyMux struct {
}
func (p *MyMux) ServeHTTP(w http.ResponseWriter, ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.5.6/mongodb/main.go | de/code/src/apps/ch.5.6/mongodb/main.go | // Example code for Chapter 5.6 from "Build Web Application with Golang"
// Purpose: Shows you have to perform basic CRUD operations for a mongodb driver.
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Person struct {
Name string
Phone string
}
func checkError(err error) {
if er... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.5.6/redis/main.go | de/code/src/apps/ch.5.6/redis/main.go | // Example code for Chapter 5.6 from "Build Web Application with Golang"
// Purpose: Shows you have to perform basic CRUD operations for a redis driver.
package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
const (
DB_PORT = "9191"
DB_URL = "1... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.5/attach_methods_to_struct/main.go | de/code/src/apps/ch.2.5/attach_methods_to_struct/main.go | // Example code from Chapter 2.5
// Attach method to struct.
package main
import (
"fmt"
"math"
)
type Rectangle struct {
width, height float64
}
type Circle struct {
radius float64
}
func (r Rectangle) area() float64 {
return r.width * r.height
}
func (c Circle) area() float64 {
return c.radius * c.radius *... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.5/method_overload/main.go | de/code/src/apps/ch.2.5/method_overload/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human
school string
}
type Employee struct {
Human
company string
}
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
func (e *Employee) SayHi() {
fmt.Pri... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.5/pass_struct_to_method/main.go | de/code/src/apps/ch.2.5/pass_struct_to_method/main.go | package main
import "fmt"
type Rectangle struct {
width, height float64
}
func area(r Rectangle) float64 {
return r.width * r.height
}
func main() {
r1 := Rectangle{12, 2}
r2 := Rectangle{9, 4}
fmt.Println("Area of r1 is: ", area(r1))
fmt.Println("Area of r2 is: ", area(r2))
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.5/box_example/main.go | de/code/src/apps/ch.2.5/box_example/main.go | package main
import "fmt"
const (
WHITE = iota
BLACK
BLUE
RED
YELLOW
)
type Color byte
type Box struct {
width, height, depth float64
color Color
}
type BoxList []Box //a slice of boxes
func (b Box) Volume() float64 {
return b.width * b.height * b.depth
}
func (b *Box) SetColor(c Color) {
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.5/embedded_method/main.go | de/code/src/apps/ch.2.5/embedded_method/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human // anonymous field
school string
}
type Employee struct {
Human
company string
}
// define a method in Human
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.6/switch_type_check/main.go | de/code/src/apps/ch.2.6/switch_type_check/main.go | package main
import (
"fmt"
"strconv"
)
type Element interface{}
type List []Element
type Person struct {
name string
age int
}
func (p Person) String() string {
return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}
func main() {
list := make(List, 3)
list[0] = 1 ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.6/reflection/main.go | de/code/src/apps/ch.2.6/reflection/main.go | package main
import (
"fmt"
"reflect"
)
func show_interface_none() {
fmt.Println("\nshow_interface_none()")
var a interface{}
a = "string"
a = 1
a = false
fmt.Println("a =", a)
}
func show_reflection() {
fmt.Println("\nshow_reflection()")
var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.6/interface/main.go | de/code/src/apps/ch.2.6/interface/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human
school string
loan float32
}
type Employee struct {
Human
company string
money float32
}
func (h Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.6/stringer_interface/main.go | de/code/src/apps/ch.2.6/stringer_interface/main.go | package main
import (
"fmt"
"strconv"
)
type Human struct {
name string
age int
phone string
}
// Human implemented fmt.Stringer
func (h Human) String() string {
return "Name:" + h.name + ", Age:" + strconv.Itoa(h.age) + " years, Contact:" + h.phone
}
func main() {
Bob := Human{"Bob", 39, "000-7777-XXX"}
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.6/type_check/main.go | de/code/src/apps/ch.2.6/type_check/main.go | package main
import (
"fmt"
"strconv"
)
type Element interface{}
type List []Element
type Person struct {
name string
age int
}
func (p Person) String() string {
return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}
func main() {
list := make(List, 3)
list[0] = ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.3.2/main.go | de/code/src/apps/ch.3.2/main.go | // Example code for Chapter 3.2 from "Build Web Application with Golang"
// Purpose: Shows how to acces the form values from the request
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() // parse arguments, you have to call th... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.4.2/main.go | de/code/src/apps/ch.4.2/main.go | // Example code for Chapter 4.2 from "Build Web Application with Golang"
// Purpose: Shows how to perform server-side validation of user input from a form.
// Also shows to use multiple template files with predefined template names.
// Run `go run main.go` and then access http://localhost:9090
package main
import (
"... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.4.2/validator/main.go | de/code/src/apps/ch.4.2/validator/main.go | // This file contains all the validators to validate the profile page.
package validator
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type ProfilePage struct {
Form *url.Values
}
type Errors struct {
Errors []error
}
// Goes through the form object and validates each element.
// A... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.2/main.go | de/code/src/apps/ch.2.2/main.go | // Example code for Chapter 2.2 from "Build Web Application with Golang"
// Purpose: Goes over the assignment and manipulation of basic data types.
package main
import (
"errors"
"fmt"
)
// constants
const Pi = 3.1415926
// booleans default to `false`
var isActive bool // global variable
var enab... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.2/what_is_wrong_with_this/main.go | de/code/src/apps/ch.2.2/what_is_wrong_with_this/main.go | // Example code for Chapter 2.2 from "Build Web Application with Golang"
// Purpose: Try to fix this program.
// From the console, type `go run main.go`
package main
func main() {
var i int
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.4/main.go | de/code/src/apps/ch.2.4/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows different ways of creating a struct
package main
import "fmt"
func show_basic_struct() {
fmt.Println("\nshow_basic_struct()")
type person struct {
name string
age int
}
var P person // p is person type
P.name = "Ast... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.4/embedded_structs_with_name_conflict/main.go | de/code/src/apps/ch.2.4/embedded_structs_with_name_conflict/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows a name conflict with a embedded field
package main
import "fmt"
type Human struct {
name string
age int
phone string // Human has phone field
}
type Employee struct {
Human // embedded field Human
speciality strin... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.4/embedded_structs/main.go | de/code/src/apps/ch.2.4/embedded_structs/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Example of embedded fields
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // anonymous field, it means Student struct includes all fields that Human has.
spe... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.4/compare_age/main.go | de/code/src/apps/ch.2.4/compare_age/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows you how to pass and use structs.
package main
import "fmt"
// define a new type
type person struct {
name string
age int
}
// compare age of two people, return the older person and differences of age
// struct is passed by ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/apps/ch.2.4/embedded_structs2/main.go | de/code/src/apps/ch.2.4/embedded_structs2/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Another example of embedded fields
package main
import "fmt"
type Skills []string
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // struct as embedded field
Skills // string slic... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/de/code/src/mymath/sqrt.go | de/code/src/mymath/sqrt.go | // Example code for Chapter 1.2 from "Build Web Application with Golang"
// Purpose: Shows how to create a simple package called `mymath`
// This package must be imported from another go file to run.
package mymath
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
ret... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/build.go | th/build.go | package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/fairlyblank/md2min"
)
// 定义一个访问者结构体
type Visitor struct{}
func (self *Visitor) md2html(arg map[string]string) error {
from := arg["from"]
to := arg["to"]
err := filepath.Walk(from+"/", func(path string, f os.FileIn... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/src/1.2/sqrt.go | th/src/1.2/sqrt.go | // 章节 1.2
// $GOPATH/src/mymath/sqrt.go
package mymath
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
return z
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/src/1.2/main.go | th/src/1.2/main.go | // 章节 1.2
// $GOPATH/src/mathapp/main.go
package main
import (
"fmt"
"mymath"
)
func main() {
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.4/main.go | th/code/src/apps/ch.5.4/main.go | // Example code for Chapter 5.4 from "Build Web Application with Golang"
// Purpose: Show how to perform CRUD operations using a postgres driver
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/lib/pq"
)
const (
DB_USER = "user"
DB_PASSWORD = ""
DB_NAME = "test"
)
func main() {
dbinfo... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.1/main.go | th/code/src/apps/ch.4.1/main.go | // Example code for Chapter 4.1 from "Build Web Application with Golang"
// Purpose: Shows how to create a simple login using a template
// Run: `go run main.go`, then access `http://localhost:9090` and `http://localhost:9090/login`
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sa... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.5/main.go | th/code/src/apps/ch.5.5/main.go | // Example code for Chapter 5.5
// Purpose is to show to use BeeDB ORM for basic CRUD operations for sqlite3
package main
import (
"database/sql"
"fmt"
"github.com/astaxie/beedb"
_ "github.com/mattn/go-sqlite3"
"time"
)
var orm beedb.Model
type Userinfo struct {
Uid int `beedb:"PK"`
Username string
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.1/main.go | th/code/src/apps/ch.2.1/main.go | // Example code for Chapter ? from "Build Web Application with Golang"
// Purpose: Hello world example demonstrating UTF-8 support.
// To run in the console, type `go run main.go`
// You're missing language fonts, if you're seeing squares or question marks.
package main
import "fmt"
func main() {
fmt.Printf("Hell... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.5/main.go | th/code/src/apps/ch.4.5/main.go | // Example code for Chapter 4.5
// Purpose is to create a server to handle uploading files.
package main
import (
"apps/ch.4.4/nonce"
"apps/ch.4.4/validator"
"fmt"
"html/template"
"io"
"mime/multipart"
"net/http"
"os"
)
const MiB_UNIT = 1 << 20
var t *template.Template
var submissions nonce.Nonces = nonce.Ne... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.5/nonce/main.go | th/code/src/apps/ch.4.5/nonce/main.go | // A nonce is a number or string used only once.
// This is useful for generating a unique token for login pages to prevent duplicate submissions.
package nonce
import (
"crypto/md5"
"errors"
"fmt"
"io"
"math/rand"
"strconv"
"time"
)
// Contains a unique token
type Nonce struct {
Token string
}
// Keeps trac... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.5/validator/main.go | th/code/src/apps/ch.4.5/validator/main.go | // This file contains all the validators to validate the profile page.
package validator
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type ProfilePage struct {
Form *url.Values
}
type Errors struct {
Errors []error
}
// Goes through the form object and validates each element.
// A... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.5/client_upload/main.go | th/code/src/apps/ch.4.5/client_upload/main.go | package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
func postFile(filename string, targetUrl string) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.Creat... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.3/main.go | th/code/src/apps/ch.5.3/main.go | // Example code for Chapter 5.3 from "Build Web Application with Golang"
// Purpose: Shows how to run simple CRUD operations using a sqlite driver
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"time"
)
const DB_PATH = "./foo.db"
func main() {
db, err := sql.Open("sqlite3", DB_PATH)
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.4/main.go | th/code/src/apps/ch.4.4/main.go | // Example code for Chapter 3.2 from "Build Web Application with Golang"
// Purpose: Shows how to prevent duplicate submissions by using tokens
// Example code for Chapter 4.4 based off the code from Chapter 4.2
// Run `go run main.go` then access http://localhost:9090
package main
import (
"apps/ch.4.4/nonce"
"apps... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.4/nonce/main.go | th/code/src/apps/ch.4.4/nonce/main.go | // A nonce is a number or string used only once.
// This is useful for generating a unique token for login pages to prevent duplicate submissions.
package nonce
import (
"crypto/md5"
"errors"
"fmt"
"io"
"math/rand"
"strconv"
"time"
)
// Contains a unique token
type Nonce struct {
Token string
}
// Keeps trac... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.4/validator/main.go | th/code/src/apps/ch.4.4/validator/main.go | // This file contains all the validators to validate the profile page.
package validator
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type ProfilePage struct {
Form *url.Values
}
type Errors struct {
Errors []error
}
// Goes through the form object and validates each element.
// A... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.2/main.go | th/code/src/apps/ch.5.2/main.go | // Example code for Chapter 5.2 from "Build Web Application with Golang"
// Purpose: Use SQL driver to perform simple CRUD operations.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
const (
DB_USER = "user"
DB_PASSWORD = ""
DB_NAME = "test"
)
func main() {
dbSouce := f... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/main.go | th/code/src/apps/ch.2.3/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Goes over if, else, switch conditions, loops and defer.
package main
import "fmt"
func computedValue() int {
return 1
}
func show_if() {
fmt.Println("\n#show_if()")
x := computedValue()
integer := 23
fmt.Println("x =", x)
fmt.... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/import_packages/main.go | th/code/src/apps/ch.2.3/import_packages/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows different ways of importing a package.
// Note: For the package `only_call_init`, we reference the path from the
// base directory of `$GOPATH/src`. The reason being Golang discourage
// the use of relative paths when import pack... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/import_packages/only_call_init/only_call_init.go | th/code/src/apps/ch.2.3/import_packages/only_call_init/only_call_init.go | package only_call_init
import "fmt"
func init() {
fmt.Println("only_call_init.init() was called.")
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/type_function/main.go | th/code/src/apps/ch.2.3/type_function/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows how to define a function type
package main
import "fmt"
type testInt func(int) bool // define a function type of variable
func isOdd(integer int) bool {
if integer%2 == 0 {
return false
}
return true
}
func isEven(intege... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/variadic_functions/main.go | th/code/src/apps/ch.2.3/variadic_functions/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows how to return multiple values from a function
package main
import "fmt"
// return results of A + B and A * B
func SumAndProduct(A, B int) (int, int) {
return A + B, A * B
}
func main() {
x := 3
y := 4
xPLUSy, xTIMESy := S... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/hidden_print_methods/main.go | th/code/src/apps/ch.2.3/hidden_print_methods/main.go | // As of Google go 1.1.2, `println()` and `print()` are hidden functions included from the runtime package.
// However it's encouraged to use the print functions from the `fmt` package.
package main
import "fmt"
func f() {
fmt.Println("First")
print("Second ")
println(" Third")
}
func main() {
f()
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/panic_and_recover/main.go | th/code/src/apps/ch.2.3/panic_and_recover/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Showing how to use `panic()` and `recover()`
package main
import (
"fmt"
"os"
)
var user = os.Getenv("USER")
func check_user() {
if user == "" {
panic("no value for $USER")
}
fmt.Println("Environment Variable `USER` =", user)... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/basic_functions/main.go | th/code/src/apps/ch.2.3/basic_functions/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Creating a basic function
package main
import "fmt"
// return greater value between a and b
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
x := 3
y := 4
z := 5
max_xy := max(x, y) // call function ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go | th/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go | // Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Shows passing a variable by value and reference
package main
import "fmt"
func add_by_value(a int) int {
a = a + 1
return a
}
func add_by_reference(a *int) int {
*a = *a + 1
return *a
}
func show_add_by_value() {
x := 3
fmt... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.3/main.go | th/code/src/apps/ch.4.3/main.go | // Example code for Chapter 4.3 from "Build Web Application with Golang"
// Purpose: Shows how to properly escape input
package main
import (
"html/template"
"net/http"
textTemplate "text/template"
)
var t *template.Template = template.Must(template.ParseFiles("index.gtpl"))
func index(w http.ResponseWriter, r *h... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.1.2/main.go | th/code/src/apps/ch.1.2/main.go | // Example code for Chapter 1.2 from "Build Web Application with Golang"
// Purpose: Run this file to check if your workspace is setup correctly.
// To run, navigate to the current directory in a console and type `go run main.go`
// If the text "Hello World" isn't shown, then setup your workspace again.
package main
i... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/select_channel/main.go | th/code/src/apps/ch.2.7/select_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to use `select`
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 1, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/buffered_channel/main.go | th/code/src/apps/ch.2.7/buffered_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to use a buffered channel
package main
import "fmt"
func main() {
c := make(chan int, 2) // change 2 to 1 will have runtime error, but 3 is fine
c <- 1
c <- 2
fmt.Println(<-c)
fmt.Println(<-c)
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/goroutine/main.go | th/code/src/apps/ch.2.7/goroutine/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to launch a simple gorountine
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world") // create a new goroutine
say... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/range_and_close_channel/main.go | th/code/src/apps/ch.2.7/range_and_close_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to close and interate through a channel
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 1, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/unbuffered_channel/main.go | th/code/src/apps/ch.2.7/unbuffered_channel/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to create and use a unbuffered channel
package main
import "fmt"
func sum(a []int, c chan int) {
total := 0
for _, v := range a {
total += v
}
c <- total // send total to c
}
func main() {
a := []int{7, 2, 8, -9, 4,... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.7/timeout/main.go | th/code/src/apps/ch.2.7/timeout/main.go | // Example code for Chapter 2.7 from "Build Web Application with Golang"
// Purpose: Shows how to create and use a timeout
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v := <-c:
fmt.Println(v)
case <-time.After(5 * tim... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.3.4/main.go | th/code/src/apps/ch.3.4/main.go | // Example code for Chapter 3.4 from "Build Web Application with Golang"
// Purpose: Shows how to create a handler for `http.ListenAndServe()`
// Run `go run main.go` then access `http://localhost:9090`
package main
import (
"fmt"
"net/http"
)
type MyMux struct {
}
func (p *MyMux) ServeHTTP(w http.ResponseWriter, ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.6/mongodb/main.go | th/code/src/apps/ch.5.6/mongodb/main.go | // Example code for Chapter 5.6 from "Build Web Application with Golang"
// Purpose: Shows you have to perform basic CRUD operations for a mongodb driver.
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Person struct {
Name string
Phone string
}
func checkError(err error) {
if er... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.5.6/redis/main.go | th/code/src/apps/ch.5.6/redis/main.go | // Example code for Chapter 5.6 from "Build Web Application with Golang"
// Purpose: Shows you have to perform basic CRUD operations for a redis driver.
package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
const (
DB_PORT = "9191"
DB_URL = "1... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.5/attach_methods_to_struct/main.go | th/code/src/apps/ch.2.5/attach_methods_to_struct/main.go | // Example code from Chapter 2.5
// Attach method to struct.
package main
import (
"fmt"
"math"
)
type Rectangle struct {
width, height float64
}
type Circle struct {
radius float64
}
func (r Rectangle) area() float64 {
return r.width * r.height
}
func (c Circle) area() float64 {
return c.radius * c.radius *... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.5/method_overload/main.go | th/code/src/apps/ch.2.5/method_overload/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human
school string
}
type Employee struct {
Human
company string
}
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
func (e *Employee) SayHi() {
fmt.Pri... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.5/pass_struct_to_method/main.go | th/code/src/apps/ch.2.5/pass_struct_to_method/main.go | package main
import "fmt"
type Rectangle struct {
width, height float64
}
func area(r Rectangle) float64 {
return r.width * r.height
}
func main() {
r1 := Rectangle{12, 2}
r2 := Rectangle{9, 4}
fmt.Println("Area of r1 is: ", area(r1))
fmt.Println("Area of r2 is: ", area(r2))
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.5/box_example/main.go | th/code/src/apps/ch.2.5/box_example/main.go | package main
import "fmt"
const (
WHITE = iota
BLACK
BLUE
RED
YELLOW
)
type Color byte
type Box struct {
width, height, depth float64
color Color
}
type BoxList []Box //a slice of boxes
func (b Box) Volume() float64 {
return b.width * b.height * b.depth
}
func (b *Box) SetColor(c Color) {
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.5/embedded_method/main.go | th/code/src/apps/ch.2.5/embedded_method/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human // anonymous field
school string
}
type Employee struct {
Human
company string
}
// define a method in Human
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.6/switch_type_check/main.go | th/code/src/apps/ch.2.6/switch_type_check/main.go | package main
import (
"fmt"
"strconv"
)
type Element interface{}
type List []Element
type Person struct {
name string
age int
}
func (p Person) String() string {
return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}
func main() {
list := make(List, 3)
list[0] = 1 ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.6/reflection/main.go | th/code/src/apps/ch.2.6/reflection/main.go | package main
import (
"fmt"
"reflect"
)
func show_interface_none() {
fmt.Println("\nshow_interface_none()")
var a interface{}
a = "string"
a = 1
a = false
fmt.Println("a =", a)
}
func show_reflection() {
fmt.Println("\nshow_reflection()")
var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.6/interface/main.go | th/code/src/apps/ch.2.6/interface/main.go | package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human
school string
loan float32
}
type Employee struct {
Human
company string
money float32
}
func (h Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.6/stringer_interface/main.go | th/code/src/apps/ch.2.6/stringer_interface/main.go | package main
import (
"fmt"
"strconv"
)
type Human struct {
name string
age int
phone string
}
// Human implemented fmt.Stringer
func (h Human) String() string {
return "Name:" + h.name + ", Age:" + strconv.Itoa(h.age) + " years, Contact:" + h.phone
}
func main() {
Bob := Human{"Bob", 39, "000-7777-XXX"}
... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.6/type_check/main.go | th/code/src/apps/ch.2.6/type_check/main.go | package main
import (
"fmt"
"strconv"
)
type Element interface{}
type List []Element
type Person struct {
name string
age int
}
func (p Person) String() string {
return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}
func main() {
list := make(List, 3)
list[0] = ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.3.2/main.go | th/code/src/apps/ch.3.2/main.go | // Example code for Chapter 3.2 from "Build Web Application with Golang"
// Purpose: Shows how to acces the form values from the request
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() // parse arguments, you have to call th... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.2/main.go | th/code/src/apps/ch.4.2/main.go | // Example code for Chapter 4.2 from "Build Web Application with Golang"
// Purpose: Shows how to perform server-side validation of user input from a form.
// Also shows to use multiple template files with predefined template names.
// Run `go run main.go` and then access http://localhost:9090
package main
import (
"... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.4.2/validator/main.go | th/code/src/apps/ch.4.2/validator/main.go | // This file contains all the validators to validate the profile page.
package validator
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type ProfilePage struct {
Form *url.Values
}
type Errors struct {
Errors []error
}
// Goes through the form object and validates each element.
// A... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.2/main.go | th/code/src/apps/ch.2.2/main.go | // Example code for Chapter 2.2 from "Build Web Application with Golang"
// Purpose: Goes over the assignment and manipulation of basic data types.
package main
import (
"errors"
"fmt"
)
// constants
const Pi = 3.1415926
// booleans default to `false`
var isActive bool // global variable
var enab... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.2/what_is_wrong_with_this/main.go | th/code/src/apps/ch.2.2/what_is_wrong_with_this/main.go | // Example code for Chapter 2.2 from "Build Web Application with Golang"
// Purpose: Try to fix this program.
// From the console, type `go run main.go`
package main
func main() {
var i int
}
| go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.4/main.go | th/code/src/apps/ch.2.4/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows different ways of creating a struct
package main
import "fmt"
func show_basic_struct() {
fmt.Println("\nshow_basic_struct()")
type person struct {
name string
age int
}
var P person // p is person type
P.name = "Ast... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.4/embedded_structs_with_name_conflict/main.go | th/code/src/apps/ch.2.4/embedded_structs_with_name_conflict/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows a name conflict with a embedded field
package main
import "fmt"
type Human struct {
name string
age int
phone string // Human has phone field
}
type Employee struct {
Human // embedded field Human
speciality strin... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.4/embedded_structs/main.go | th/code/src/apps/ch.2.4/embedded_structs/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Example of embedded fields
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // anonymous field, it means Student struct includes all fields that Human has.
spe... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.4/compare_age/main.go | th/code/src/apps/ch.2.4/compare_age/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows you how to pass and use structs.
package main
import "fmt"
// define a new type
type person struct {
name string
age int
}
// compare age of two people, return the older person and differences of age
// struct is passed by ... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/apps/ch.2.4/embedded_structs2/main.go | th/code/src/apps/ch.2.4/embedded_structs2/main.go | // Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Another example of embedded fields
package main
import "fmt"
type Skills []string
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // struct as embedded field
Skills // string slic... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/th/code/src/mymath/sqrt.go | th/code/src/mymath/sqrt.go | // Example code for Chapter 1.2 from "Build Web Application with Golang"
// Purpose: Shows how to create a simple package called `mymath`
// This package must be imported from another go file to run.
package mymath
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
ret... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
astaxie/build-web-application-with-golang | https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/zh/a_herf.go | zh/a_herf.go | package main
import(
"fmt"
"log"
"os"
"path/filepath"
"sort"
)
func dir()([]string,error) {
path, err := os.Getwd()
if err != nil {
fmt.Println("err is:", err)
}
log.Println(path)
path =path +"/*.html"
fmt.Println(path)
files,err := filepath.Glob(path)
var s... | go | BSD-3-Clause | c294b087b96d99d2593ad8f470151af5354bb57f | 2026-01-07T08:35:46.175972Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.