diff --git a/src/github.com/statho7/Academy/Lab1/lab1.go b/src/github.com/statho7/Academy/Lab1/lab1.go index 50dfe9e..5d1305a 100644 --- a/src/github.com/statho7/Academy/Lab1/lab1.go +++ b/src/github.com/statho7/Academy/Lab1/lab1.go @@ -5,7 +5,6 @@ import ( "strconv" ) - func product(array [5]int) int { product := array[0] @@ -20,9 +19,8 @@ func product(array [5]int) int { return product } - func slicemania(mySlice []string) (int, string) { - + length := (len(mySlice) - 1) var total int total = 1 @@ -40,7 +38,7 @@ func slicemania(mySlice []string) (int, string) { return total, myString } -func main() { +func main() { myArray := [5]int{1, 2, 3, 4, 5} mySlice := []string{"1", "4", "293", "4", "9", "hello", "sunshine"} @@ -52,4 +50,4 @@ func main() { res2, res3 := slicemania(mySlice) fmt.Println(res2, res3) -} \ No newline at end of file +} diff --git a/src/github.com/statho7/Academy/Lab2/lab2.go b/src/github.com/statho7/Academy/Lab2/lab2.go index 2e4dec9..f7bc074 100644 --- a/src/github.com/statho7/Academy/Lab2/lab2.go +++ b/src/github.com/statho7/Academy/Lab2/lab2.go @@ -16,10 +16,10 @@ func (r *Circle) area() float64 { func (r Circle) circumference() float64 { pi := math.Pi - return float64(2)*pi * float64(r.radius) + return float64(2) * pi * float64(r.radius) } -func main() { +func main() { r := Circle{diameter: 10, radius: 5} fmt.Println("area: ", r.area()) @@ -28,4 +28,4 @@ func main() { rp := &r fmt.Println("area: ", rp.area()) fmt.Println("perim:", rp.circumference()) -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoByExample/Advanced/atomic_counters/atomic_counters.go b/src/github.com/statho7/GoByExample/Advanced/atomic_counters/atomic_counters.go index f9b87c9..becc617 100644 --- a/src/github.com/statho7/GoByExample/Advanced/atomic_counters/atomic_counters.go +++ b/src/github.com/statho7/GoByExample/Advanced/atomic_counters/atomic_counters.go @@ -1,30 +1,30 @@ package main import ( - "fmt" - "sync" - "sync/atomic" + "fmt" + "sync" + "sync/atomic" ) func main() { - var ops uint64 + var ops uint64 - var wg sync.WaitGroup + var wg sync.WaitGroup - for i := 0; i < 50; i++ { - wg.Add(1) + for i := 0; i < 100; i++ { + wg.Add(1) - go func() { - for c := 0; c < 1000; c++ { + go func() { + for c := 0; c < 1000; c++ { - atomic.AddUint64(&ops, 1) - } - wg.Done() - }() - } + atomic.AddUint64(&ops, 2) + } + wg.Done() + }() + } - wg.Wait() + wg.Wait() - fmt.Println("ops:", ops) -} \ No newline at end of file + fmt.Println("ops:", ops) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/channel_buffering/channel_buffering.go b/src/github.com/statho7/GoByExample/Advanced/channel_buffering/channel_buffering.go index a37b391..d64bec1 100644 --- a/src/github.com/statho7/GoByExample/Advanced/channel_buffering/channel_buffering.go +++ b/src/github.com/statho7/GoByExample/Advanced/channel_buffering/channel_buffering.go @@ -4,11 +4,11 @@ import "fmt" func main() { - messages := make(chan string, 2) + messages := make(chan string, 2) - messages <- "buffered" - messages <- "channel" + messages <- "buffered" + messages <- "channel" - fmt.Println(<-messages) - fmt.Println(<-messages) -} \ No newline at end of file + fmt.Println(<-messages) + fmt.Println(<-messages) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/channel_directions/channel_directions.go b/src/github.com/statho7/GoByExample/Advanced/channel_directions/channel_directions.go index 6730840..7006fe2 100644 --- a/src/github.com/statho7/GoByExample/Advanced/channel_directions/channel_directions.go +++ b/src/github.com/statho7/GoByExample/Advanced/channel_directions/channel_directions.go @@ -3,18 +3,18 @@ package main import "fmt" func ping(pings chan<- string, msg string) { - pings <- msg + pings <- msg } func pong(pings <-chan string, pongs chan<- string) { - msg := <-pings - pongs <- msg + msg := <-pings + pongs <- msg } func main() { - pings := make(chan string, 1) - pongs := make(chan string, 1) - ping(pings, "passed message") - pong(pings, pongs) - fmt.Println(<-pongs) -} \ No newline at end of file + pings := make(chan string, 1) + pongs := make(chan string, 1) + ping(pings, "passed message") + pong(pings, pongs) + fmt.Println(<-pongs) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/channel_synchronization/channel_synchronization.go b/src/github.com/statho7/GoByExample/Advanced/channel_synchronization/channel_synchronization.go index 4ed51d2..bb04b56 100644 --- a/src/github.com/statho7/GoByExample/Advanced/channel_synchronization/channel_synchronization.go +++ b/src/github.com/statho7/GoByExample/Advanced/channel_synchronization/channel_synchronization.go @@ -1,22 +1,22 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func worker(done chan bool) { - fmt.Print("working...") - time.Sleep(time.Second) - fmt.Println("done") + fmt.Print("working...") + time.Sleep(time.Second) + fmt.Println("done") - done <- true + done <- true } func main() { - done := make(chan bool, 1) - go worker(done) + done := make(chan bool, 1) + go worker(done) - <-done -} \ No newline at end of file + <-done +} diff --git a/src/github.com/statho7/GoByExample/Advanced/channels/channels.go b/src/github.com/statho7/GoByExample/Advanced/channels/channels.go index 8f4d212..c00f693 100644 --- a/src/github.com/statho7/GoByExample/Advanced/channels/channels.go +++ b/src/github.com/statho7/GoByExample/Advanced/channels/channels.go @@ -4,10 +4,10 @@ import "fmt" func main() { - messages := make(chan string) + messages := make(chan string) - go func() { messages <- "ping" }() + go func() { messages <- "ping" }() - msg := <-messages - fmt.Println(msg) -} \ No newline at end of file + msg := <-messages + fmt.Println(msg) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/closing_channels/closing_channels.go b/src/github.com/statho7/GoByExample/Advanced/closing_channels/closing_channels.go index 546bf91..86c0448 100644 --- a/src/github.com/statho7/GoByExample/Advanced/closing_channels/closing_channels.go +++ b/src/github.com/statho7/GoByExample/Advanced/closing_channels/closing_channels.go @@ -3,28 +3,28 @@ package main import "fmt" func main() { - jobs := make(chan int, 5) - done := make(chan bool) + jobs := make(chan int, 5) + done := make(chan bool) - go func() { - for { - j, more := <-jobs - if more { - fmt.Println("received job", j) - } else { - fmt.Println("received all jobs") - done <- true - return - } - } - }() + go func() { + for { + j, more := <-jobs + if more { + fmt.Println("received job", j) + } else { + fmt.Println("received all jobs") + done <- true + return + } + } + }() - for j := 1; j <= 3; j++ { - jobs <- j - fmt.Println("sent job", j) - } - close(jobs) - fmt.Println("sent all jobs") + for j := 1; j <= 3; j++ { + jobs <- j + fmt.Println("sent job", j) + } + close(jobs) + fmt.Println("sent all jobs") - <-done -} \ No newline at end of file + <-done +} diff --git a/src/github.com/statho7/GoByExample/Advanced/context/context.go b/src/github.com/statho7/GoByExample/Advanced/context/context.go index c5f871c..34530be 100644 --- a/src/github.com/statho7/GoByExample/Advanced/context/context.go +++ b/src/github.com/statho7/GoByExample/Advanced/context/context.go @@ -1,31 +1,31 @@ package main import ( - "fmt" - "net/http" - "time" + "fmt" + "net/http" + "time" ) func hello(w http.ResponseWriter, req *http.Request) { - ctx := req.Context() - fmt.Println("server: hello handler started") - defer fmt.Println("server: hello handler ended") + ctx := req.Context() + fmt.Println("server: hello handler started") + defer fmt.Println("server: hello handler ended") - select { - case <-time.After(10 * time.Second): - fmt.Fprintf(w, "hello\n") - case <-ctx.Done(): + select { + case <-time.After(10 * time.Second): + fmt.Fprintf(w, "hello\n") + case <-ctx.Done(): - err := ctx.Err() - fmt.Println("server:", err) - internalError := http.StatusInternalServerError - http.Error(w, err.Error(), internalError) - } + err := ctx.Err() + fmt.Println("server:", err) + internalError := http.StatusInternalServerError + http.Error(w, err.Error(), internalError) + } } func main() { - http.HandleFunc("/hello", hello) - http.ListenAndServe(":8090", nil) -} \ No newline at end of file + http.HandleFunc("/hello", hello) + http.ListenAndServe(":8090", nil) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/goroutines/goroutines.go b/src/github.com/statho7/GoByExample/Advanced/goroutines/goroutines.go index 9a1999a..2a48ba8 100644 --- a/src/github.com/statho7/GoByExample/Advanced/goroutines/goroutines.go +++ b/src/github.com/statho7/GoByExample/Advanced/goroutines/goroutines.go @@ -1,26 +1,26 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func f(from string) { - for i := 0; i < 3; i++ { - fmt.Println(from, ":", i) - } + for i := 0; i < 3; i++ { + fmt.Println(from, ":", i) + } } func main() { - f("direct") + f("direct") - go f("goroutine") + go f("goroutine") - go func(msg string) { - fmt.Println(msg) - }("going") + go func(msg string) { + fmt.Println(msg) + }("going") - time.Sleep(time.Second) - fmt.Println("done") -} \ No newline at end of file + time.Sleep(time.Second) + fmt.Println("done") +} diff --git a/src/github.com/statho7/GoByExample/Advanced/http_clients/http_clients.go b/src/github.com/statho7/GoByExample/Advanced/http_clients/http_clients.go index b6bc8b2..1270448 100644 --- a/src/github.com/statho7/GoByExample/Advanced/http_clients/http_clients.go +++ b/src/github.com/statho7/GoByExample/Advanced/http_clients/http_clients.go @@ -1,27 +1,27 @@ package main import ( - "bufio" - "fmt" - "net/http" + "bufio" + "fmt" + "net/http" ) func main() { - resp, err := http.Get("http://gobyexample.com") - if err != nil { - panic(err) - } - defer resp.Body.Close() + resp, err := http.Get("http://gobyexample.com") + if err != nil { + panic(err) + } + defer resp.Body.Close() - fmt.Println("Response status:", resp.Status) + fmt.Println("Response status:", resp.Status) - scanner := bufio.NewScanner(resp.Body) - for i := 0; scanner.Scan() && i < 5; i++ { - fmt.Println(scanner.Text()) - } + scanner := bufio.NewScanner(resp.Body) + for i := 0; scanner.Scan() && i < 5; i++ { + fmt.Println(scanner.Text()) + } - if err := scanner.Err(); err != nil { - panic(err) - } -} \ No newline at end of file + if err := scanner.Err(); err != nil { + panic(err) + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/http_servers/http_servers.go b/src/github.com/statho7/GoByExample/Advanced/http_servers/http_servers.go index bf0694a..44fb79d 100644 --- a/src/github.com/statho7/GoByExample/Advanced/http_servers/http_servers.go +++ b/src/github.com/statho7/GoByExample/Advanced/http_servers/http_servers.go @@ -1,28 +1,28 @@ package main import ( - "fmt" - "net/http" + "fmt" + "net/http" ) func hello(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "hello\n") + fmt.Fprintf(w, "hello\n") } func headers(w http.ResponseWriter, req *http.Request) { - for name, headers := range req.Header { - for _, h := range headers { - fmt.Fprintf(w, "%v: %v\n", name, h) - } - } + for name, headers := range req.Header { + for _, h := range headers { + fmt.Fprintf(w, "%v: %v\n", name, h) + } + } } func main() { - http.HandleFunc("/hello", hello) - http.HandleFunc("/headers", headers) + http.HandleFunc("/hello", hello) + http.HandleFunc("/headers", headers) - http.ListenAndServe(":8090", nil) -} \ No newline at end of file + http.ListenAndServe(":8090", nil) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/json/json.go b/src/github.com/statho7/GoByExample/Advanced/json/json.go index cfe6300..f3beb22 100644 --- a/src/github.com/statho7/GoByExample/Advanced/json/json.go +++ b/src/github.com/statho7/GoByExample/Advanced/json/json.go @@ -1,78 +1,78 @@ package main import ( - "encoding/json" - "fmt" - "os" + "encoding/json" + "fmt" + "os" ) type response1 struct { - Page int - Fruits []string + Page int + Fruits []string } type response2 struct { - Page int `json:"page"` - Fruits []string `json:"fruits"` + Page int `json:"page"` + Fruits []string `json:"fruits"` } func main() { - bolB, _ := json.Marshal(true) - fmt.Println(string(bolB)) + bolB, _ := json.Marshal(true) + fmt.Println(string(bolB)) - intB, _ := json.Marshal(1) - fmt.Println(string(intB)) + intB, _ := json.Marshal(1) + fmt.Println(string(intB)) - fltB, _ := json.Marshal(2.34) - fmt.Println(string(fltB)) + fltB, _ := json.Marshal(2.34) + fmt.Println(string(fltB)) - strB, _ := json.Marshal("gopher") - fmt.Println(string(strB)) + strB, _ := json.Marshal("gopher") + fmt.Println(string(strB)) - slcD := []string{"apple", "peach", "pear"} - slcB, _ := json.Marshal(slcD) - fmt.Println(string(slcB)) + slcD := []string{"apple", "peach", "pear"} + slcB, _ := json.Marshal(slcD) + fmt.Println(string(slcB)) - mapD := map[string]int{"apple": 5, "lettuce": 7} - mapB, _ := json.Marshal(mapD) - fmt.Println(string(mapB)) + mapD := map[string]int{"apple": 5, "lettuce": 7} + mapB, _ := json.Marshal(mapD) + fmt.Println(string(mapB)) - res1D := &response1{ - Page: 1, - Fruits: []string{"apple", "peach", "pear"}} - res1B, _ := json.Marshal(res1D) - fmt.Println(string(res1B)) + res1D := &response1{ + Page: 1, + Fruits: []string{"apple", "peach", "pear"}} + res1B, _ := json.Marshal(res1D) + fmt.Println(string(res1B)) - res2D := &response2{ - Page: 1, - Fruits: []string{"apple", "peach", "pear"}} - res2B, _ := json.Marshal(res2D) - fmt.Println(string(res2B)) + res2D := &response2{ + Page: 1, + Fruits: []string{"apple", "peach", "pear"}} + res2B, _ := json.Marshal(res2D) + fmt.Println(string(res2B)) - byt := []byte(`{"num":6.13,"strs":["a","b"]}`) + byt := []byte(`{"num":6.13,"strs":["a","b"]}`) - var dat map[string]interface{} + var dat map[string]interface{} - if err := json.Unmarshal(byt, &dat); err != nil { - panic(err) - } - fmt.Println(dat) + if err := json.Unmarshal(byt, &dat); err != nil { + panic(err) + } + fmt.Println(dat) - num := dat["num"].(float64) - fmt.Println(num) + num := dat["num"].(float64) + fmt.Println(num) - strs := dat["strs"].([]interface{}) - str1 := strs[0].(string) - fmt.Println(str1) + strs := dat["strs"].([]interface{}) + str1 := strs[0].(string) + fmt.Println(str1) - str := `{"page": 1, "fruits": ["apple", "peach"]}` - res := response2{} - json.Unmarshal([]byte(str), &res) - fmt.Println(res) - fmt.Println(res.Fruits[0]) + str := `{"page": 1, "fruits": ["apple", "peach"]}` + res := response2{} + json.Unmarshal([]byte(str), &res) + fmt.Println(res) + fmt.Println(res.Fruits[0]) - enc := json.NewEncoder(os.Stdout) - d := map[string]int{"apple": 5, "lettuce": 7} - enc.Encode(d) -} \ No newline at end of file + enc := json.NewEncoder(os.Stdout) + d := map[string]int{"apple": 5, "lettuce": 7} + enc.Encode(d) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/mutexes/mutexes.go b/src/github.com/statho7/GoByExample/Advanced/mutexes/mutexes.go index ac17426..3c82b0f 100644 --- a/src/github.com/statho7/GoByExample/Advanced/mutexes/mutexes.go +++ b/src/github.com/statho7/GoByExample/Advanced/mutexes/mutexes.go @@ -1,42 +1,42 @@ package main import ( - "fmt" - "sync" + "fmt" + "sync" ) type Container struct { - mu sync.Mutex - counters map[string]int + mu sync.Mutex + counters map[string]int } func (c *Container) inc(name string) { - c.mu.Lock() - defer c.mu.Unlock() - c.counters[name]++ + c.mu.Lock() + defer c.mu.Unlock() + c.counters[name]++ } func main() { - c := Container{ + c := Container{ - counters: map[string]int{"a": 0, "b": 0}, - } + counters: map[string]int{"a": 0, "b": 0}, + } - var wg sync.WaitGroup + var wg sync.WaitGroup - doIncrement := func(name string, n int) { - for i := 0; i < n; i++ { - c.inc(name) - } - wg.Done() - } + doIncrement := func(name string, n int) { + for i := 0; i < n; i++ { + c.inc(name) + } + wg.Done() + } - wg.Add(3) - go doIncrement("a", 10000) - go doIncrement("a", 10000) - go doIncrement("b", 10000) + wg.Add(3) + go doIncrement("a", 10000) + go doIncrement("a", 10000) + go doIncrement("b", 10000) - wg.Wait() - fmt.Println(c.counters) -} \ No newline at end of file + wg.Wait() + fmt.Println(c.counters) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/non_blocking_channel_operations/non_blocking_channel_operations.go b/src/github.com/statho7/GoByExample/Advanced/non_blocking_channel_operations/non_blocking_channel_operations.go index cc1b596..7e28808 100644 --- a/src/github.com/statho7/GoByExample/Advanced/non_blocking_channel_operations/non_blocking_channel_operations.go +++ b/src/github.com/statho7/GoByExample/Advanced/non_blocking_channel_operations/non_blocking_channel_operations.go @@ -3,30 +3,30 @@ package main import "fmt" func main() { - messages := make(chan string) - signals := make(chan bool) + messages := make(chan string) + signals := make(chan bool) - select { - case msg := <-messages: - fmt.Println("received message", msg) - default: - fmt.Println("no message received") - } + select { + case msg := <-messages: + fmt.Println("received message", msg) + default: + fmt.Println("no message received") + } - msg := "hi" - select { - case messages <- msg: - fmt.Println("sent message", msg) - default: - fmt.Println("no message sent") - } + msg := "hi" + select { + case messages <- msg: + fmt.Println("sent message", msg) + default: + fmt.Println("no message sent") + } - select { - case msg := <-messages: - fmt.Println("received message", msg) - case sig := <-signals: - fmt.Println("received signal", sig) - default: - fmt.Println("no activity") - } -} \ No newline at end of file + select { + case msg := <-messages: + fmt.Println("received message", msg) + case sig := <-signals: + fmt.Println("received signal", sig) + default: + fmt.Println("no activity") + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/range_over_channels/range_over_channels.go b/src/github.com/statho7/GoByExample/Advanced/range_over_channels/range_over_channels.go index 355a738..52a6e8e 100644 --- a/src/github.com/statho7/GoByExample/Advanced/range_over_channels/range_over_channels.go +++ b/src/github.com/statho7/GoByExample/Advanced/range_over_channels/range_over_channels.go @@ -4,12 +4,12 @@ import "fmt" func main() { - queue := make(chan string, 2) - queue <- "one" - queue <- "two" - close(queue) - - for elem := range queue { - fmt.Println(elem) - } -} \ No newline at end of file + queue := make(chan string, 2) + queue <- "one" + queue <- "two" + close(queue) + + for elem := range queue { + fmt.Println(elem) + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/select/select.go b/src/github.com/statho7/GoByExample/Advanced/select/select.go index 4797249..ba2aeb3 100644 --- a/src/github.com/statho7/GoByExample/Advanced/select/select.go +++ b/src/github.com/statho7/GoByExample/Advanced/select/select.go @@ -1,30 +1,30 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - c1 := make(chan string) - c2 := make(chan string) + c1 := make(chan string) + c2 := make(chan string) - go func() { - time.Sleep(1 * time.Second) - c1 <- "one" - }() - go func() { - time.Sleep(2 * time.Second) - c2 <- "two" - }() + go func() { + time.Sleep(1 * time.Second) + c1 <- "one" + }() + go func() { + time.Sleep(2 * time.Second) + c2 <- "two" + }() - for i := 0; i < 2; i++ { - select { - case msg1 := <-c1: - fmt.Println("received", msg1) - case msg2 := <-c2: - fmt.Println("received", msg2) - } - } -} \ No newline at end of file + for i := 0; i < 2; i++ { + select { + case msg1 := <-c1: + fmt.Println("received", msg1) + case msg2 := <-c2: + fmt.Println("received", msg2) + } + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/stateful_goroutines/stateful_goroutines.go b/src/github.com/statho7/GoByExample/Advanced/stateful_goroutines/stateful_goroutines.go index cf925e3..87b40dd 100644 --- a/src/github.com/statho7/GoByExample/Advanced/stateful_goroutines/stateful_goroutines.go +++ b/src/github.com/statho7/GoByExample/Advanced/stateful_goroutines/stateful_goroutines.go @@ -1,76 +1,76 @@ package main import ( - "fmt" - "math/rand" - "sync/atomic" - "time" + "fmt" + "math/rand" + "sync/atomic" + "time" ) type readOp struct { - key int - resp chan int + key int + resp chan int } type writeOp struct { - key int - val int - resp chan bool + key int + val int + resp chan bool } func main() { - var readOps uint64 - var writeOps uint64 + var readOps uint64 + var writeOps uint64 - reads := make(chan readOp) - writes := make(chan writeOp) + reads := make(chan readOp) + writes := make(chan writeOp) - go func() { - var state = make(map[int]int) - for { - select { - case read := <-reads: - read.resp <- state[read.key] - case write := <-writes: - state[write.key] = write.val - write.resp <- true - } - } - }() + go func() { + var state = make(map[int]int) + for { + select { + case read := <-reads: + read.resp <- state[read.key] + case write := <-writes: + state[write.key] = write.val + write.resp <- true + } + } + }() - for r := 0; r < 100; r++ { - go func() { - for { - read := readOp{ - key: rand.Intn(5), - resp: make(chan int)} - reads <- read - <-read.resp - atomic.AddUint64(&readOps, 1) - time.Sleep(time.Millisecond) - } - }() - } + for r := 0; r < 100; r++ { + go func() { + for { + read := readOp{ + key: rand.Intn(5), + resp: make(chan int)} + reads <- read + <-read.resp + atomic.AddUint64(&readOps, 1) + time.Sleep(time.Millisecond) + } + }() + } - for w := 0; w < 10; w++ { - go func() { - for { - write := writeOp{ - key: rand.Intn(5), - val: rand.Intn(100), - resp: make(chan bool)} - writes <- write - <-write.resp - atomic.AddUint64(&writeOps, 1) - time.Sleep(time.Millisecond) - } - }() - } + for w := 0; w < 10; w++ { + go func() { + for { + write := writeOp{ + key: rand.Intn(5), + val: rand.Intn(100), + resp: make(chan bool)} + writes <- write + <-write.resp + atomic.AddUint64(&writeOps, 1) + time.Sleep(time.Millisecond) + } + }() + } - time.Sleep(time.Second) + time.Sleep(time.Second) - readOpsFinal := atomic.LoadUint64(&readOps) - fmt.Println("readOps:", readOpsFinal) - writeOpsFinal := atomic.LoadUint64(&writeOps) - fmt.Println("writeOps:", writeOpsFinal) -} \ No newline at end of file + readOpsFinal := atomic.LoadUint64(&readOps) + fmt.Println("readOps:", readOpsFinal) + writeOpsFinal := atomic.LoadUint64(&writeOps) + fmt.Println("writeOps:", writeOpsFinal) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/tickers/tickers.go b/src/github.com/statho7/GoByExample/Advanced/tickers/tickers.go index ec44231..375aed3 100644 --- a/src/github.com/statho7/GoByExample/Advanced/tickers/tickers.go +++ b/src/github.com/statho7/GoByExample/Advanced/tickers/tickers.go @@ -1,28 +1,28 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - ticker := time.NewTicker(500 * time.Millisecond) - done := make(chan bool) + ticker := time.NewTicker(500 * time.Millisecond) + done := make(chan bool) - go func() { - for { - select { - case <-done: - return - case t := <-ticker.C: - fmt.Println("Tick at", t) - } - } - }() + go func() { + for { + select { + case <-done: + return + case t := <-ticker.C: + fmt.Println("Tick at", t) + } + } + }() - time.Sleep(1600 * time.Millisecond) - ticker.Stop() - done <- true - fmt.Println("Ticker stopped") -} \ No newline at end of file + time.Sleep(1600 * time.Millisecond) + ticker.Stop() + done <- true + fmt.Println("Ticker stopped") +} diff --git a/src/github.com/statho7/GoByExample/Advanced/timeouts/timeouts.go b/src/github.com/statho7/GoByExample/Advanced/timeouts/timeouts.go index 30dcb10..173a0ed 100644 --- a/src/github.com/statho7/GoByExample/Advanced/timeouts/timeouts.go +++ b/src/github.com/statho7/GoByExample/Advanced/timeouts/timeouts.go @@ -1,34 +1,34 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - c1 := make(chan string, 1) - go func() { - time.Sleep(2 * time.Second) - c1 <- "result 1" - }() + c1 := make(chan string, 1) + go func() { + time.Sleep(2 * time.Second) + c1 <- "result 1" + }() - select { - case res := <-c1: - fmt.Println(res) - case <-time.After(1 * time.Second): - fmt.Println("timeout 1") - } + select { + case res := <-c1: + fmt.Println(res) + case <-time.After(1 * time.Second): + fmt.Println("timeout 1") + } - c2 := make(chan string, 1) - go func() { - time.Sleep(2 * time.Second) - c2 <- "result 2" - }() - select { - case res := <-c2: - fmt.Println(res) - case <-time.After(3 * time.Second): - fmt.Println("timeout 2") - } -} \ No newline at end of file + c2 := make(chan string, 1) + go func() { + time.Sleep(2 * time.Second) + c2 <- "result 2" + }() + select { + case res := <-c2: + fmt.Println(res) + case <-time.After(3 * time.Second): + fmt.Println("timeout 2") + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/timers/timers.go b/src/github.com/statho7/GoByExample/Advanced/timers/timers.go index a8b0a5c..2f4a94f 100644 --- a/src/github.com/statho7/GoByExample/Advanced/timers/timers.go +++ b/src/github.com/statho7/GoByExample/Advanced/timers/timers.go @@ -1,26 +1,26 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - timer1 := time.NewTimer(2 * time.Second) + timer1 := time.NewTimer(2 * time.Second) - <-timer1.C - fmt.Println("Timer 1 fired") + <-timer1.C + fmt.Println("Timer 1 fired") - timer2 := time.NewTimer(time.Second) - go func() { - <-timer2.C - fmt.Println("Timer 2 fired") - }() - stop2 := timer2.Stop() - if stop2 { - fmt.Println("Timer 2 stopped") - } + timer2 := time.NewTimer(time.Second) + go func() { + <-timer2.C + fmt.Println("Timer 2 fired") + }() + stop2 := timer2.Stop() + if stop2 { + fmt.Println("Timer 2 stopped") + } - time.Sleep(2 * time.Second) -} \ No newline at end of file + time.Sleep(2 * time.Second) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/url_parsing/url_parsing.go b/src/github.com/statho7/GoByExample/Advanced/url_parsing/url_parsing.go index 225709e..f59c979 100644 --- a/src/github.com/statho7/GoByExample/Advanced/url_parsing/url_parsing.go +++ b/src/github.com/statho7/GoByExample/Advanced/url_parsing/url_parsing.go @@ -1,37 +1,37 @@ package main import ( - "fmt" - "net" - "net/url" + "fmt" + "net" + "net/url" ) func main() { - s := "postgres://user:pass@host.com:5432/path?k=v#f" + s := "postgres://user:pass@host.com:5432/path?k=v#f" - u, err := url.Parse(s) - if err != nil { - panic(err) - } + u, err := url.Parse(s) + if err != nil { + panic(err) + } - fmt.Println(u.Scheme) + fmt.Println(u.Scheme) - fmt.Println(u.User) - fmt.Println(u.User.Username()) - p, _ := u.User.Password() - fmt.Println(p) + fmt.Println(u.User) + fmt.Println(u.User.Username()) + p, _ := u.User.Password() + fmt.Println(p) - fmt.Println(u.Host) - host, port, _ := net.SplitHostPort(u.Host) - fmt.Println(host) - fmt.Println(port) + fmt.Println(u.Host) + host, port, _ := net.SplitHostPort(u.Host) + fmt.Println(host) + fmt.Println(port) - fmt.Println(u.Path) - fmt.Println(u.Fragment) + fmt.Println(u.Path) + fmt.Println(u.Fragment) - fmt.Println(u.RawQuery) - m, _ := url.ParseQuery(u.RawQuery) - fmt.Println(m) - fmt.Println(m["k"][0]) -} \ No newline at end of file + fmt.Println(u.RawQuery) + m, _ := url.ParseQuery(u.RawQuery) + fmt.Println(m) + fmt.Println(m["k"][0]) +} diff --git a/src/github.com/statho7/GoByExample/Advanced/waitgroups/waitgroups.go b/src/github.com/statho7/GoByExample/Advanced/waitgroups/waitgroups.go index c0c2a22..8133c2a 100644 --- a/src/github.com/statho7/GoByExample/Advanced/waitgroups/waitgroups.go +++ b/src/github.com/statho7/GoByExample/Advanced/waitgroups/waitgroups.go @@ -1,33 +1,33 @@ package main import ( - "fmt" - "sync" - "time" + "fmt" + "sync" + "time" ) func worker(id int) { - fmt.Printf("Worker %d starting\n", id) + fmt.Printf("Worker %d starting\n", id) - time.Sleep(time.Second) - fmt.Printf("Worker %d done\n", id) + time.Sleep(time.Second) + fmt.Printf("Worker %d done\n", id) } func main() { - var wg sync.WaitGroup + var wg sync.WaitGroup - for i := 1; i <= 5; i++ { - wg.Add(1) + for i := 1; i <= 5; i++ { + wg.Add(1) - i := i + i := i - go func() { - defer wg.Done() - worker(i) - }() - } + go func() { + defer wg.Done() + worker(i) + }() + } - wg.Wait() + wg.Wait() -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoByExample/Advanced/worker_pools/worker_pools.go b/src/github.com/statho7/GoByExample/Advanced/worker_pools/worker_pools.go index 5cb43ae..c0102c7 100644 --- a/src/github.com/statho7/GoByExample/Advanced/worker_pools/worker_pools.go +++ b/src/github.com/statho7/GoByExample/Advanced/worker_pools/worker_pools.go @@ -1,35 +1,35 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func worker(id int, jobs <-chan int, results chan<- int) { - for j := range jobs { - fmt.Println("worker", id, "started job", j) - time.Sleep(time.Second) - fmt.Println("worker", id, "finished job", j) - results <- j * 2 - } + for j := range jobs { + fmt.Println("worker", id, "started job", j) + time.Sleep(time.Second) + fmt.Println("worker", id, "finished job", j) + results <- j * 2 + } } func main() { - const numJobs = 5 - jobs := make(chan int, numJobs) - results := make(chan int, numJobs) + const numJobs = 5 + jobs := make(chan int, numJobs) + results := make(chan int, numJobs) - for w := 1; w <= 3; w++ { - go worker(w, jobs, results) - } + for w := 1; w <= 3; w++ { + go worker(w, jobs, results) + } - for j := 1; j <= numJobs; j++ { - jobs <- j - } - close(jobs) + for j := 1; j <= numJobs; j++ { + jobs <- j + } + close(jobs) - for a := 1; a <= numJobs; a++ { - <-results - } -} \ No newline at end of file + for a := 1; a <= numJobs; a++ { + <-results + } +} diff --git a/src/github.com/statho7/GoByExample/Advanced/xml/xml.go b/src/github.com/statho7/GoByExample/Advanced/xml/xml.go index 37bf466..aa91824 100644 --- a/src/github.com/statho7/GoByExample/Advanced/xml/xml.go +++ b/src/github.com/statho7/GoByExample/Advanced/xml/xml.go @@ -1,48 +1,48 @@ package main import ( - "encoding/xml" - "fmt" + "encoding/xml" + "fmt" ) type Plant struct { - XMLName xml.Name `xml:"plant"` - Id int `xml:"id,attr"` - Name string `xml:"name"` - Origin []string `xml:"origin"` + XMLName xml.Name `xml:"plant"` + Id int `xml:"id,attr"` + Name string `xml:"name"` + Origin []string `xml:"origin"` } func (p Plant) String() string { - return fmt.Sprintf("Plant id=%v, name=%v, origin=%v", - p.Id, p.Name, p.Origin) + return fmt.Sprintf("Plant id=%v, name=%v, origin=%v", + p.Id, p.Name, p.Origin) } func main() { - coffee := &Plant{Id: 27, Name: "Coffee"} - coffee.Origin = []string{"Ethiopia", "Brazil"} + coffee := &Plant{Id: 27, Name: "Coffee"} + coffee.Origin = []string{"Ethiopia", "Brazil"} - out, _ := xml.MarshalIndent(coffee, " ", " ") - fmt.Println(string(out)) + out, _ := xml.MarshalIndent(coffee, " ", " ") + fmt.Println(string(out)) - fmt.Println(xml.Header + string(out)) + fmt.Println(xml.Header + string(out)) - var p Plant - if err := xml.Unmarshal(out, &p); err != nil { - panic(err) - } - fmt.Println(p) + var p Plant + if err := xml.Unmarshal(out, &p); err != nil { + panic(err) + } + fmt.Println(p) - tomato := &Plant{Id: 81, Name: "Tomato"} - tomato.Origin = []string{"Mexico", "California"} + tomato := &Plant{Id: 81, Name: "Tomato"} + tomato.Origin = []string{"Mexico", "California"} - type Nesting struct { - XMLName xml.Name `xml:"nesting"` - Plants []*Plant `xml:"parent>child>plant"` - } + type Nesting struct { + XMLName xml.Name `xml:"nesting"` + Plants []*Plant `xml:"parent>child>plant"` + } - nesting := &Nesting{} - nesting.Plants = []*Plant{coffee, tomato} + nesting := &Nesting{} + nesting.Plants = []*Plant{coffee, tomato} - out, _ = xml.MarshalIndent(nesting, " ", " ") - fmt.Println(string(out)) -} \ No newline at end of file + out, _ = xml.MarshalIndent(nesting, " ", " ") + fmt.Println(string(out)) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/arrays/arrays.go b/src/github.com/statho7/GoByExample/BasicResources/arrays/arrays.go index 60ccfd2..e62b47f 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/arrays/arrays.go +++ b/src/github.com/statho7/GoByExample/BasicResources/arrays/arrays.go @@ -4,23 +4,23 @@ import "fmt" func main() { - var a [5]int - fmt.Println("emp:", a) + var a [5]int + fmt.Println("emp:", a) - a[4] = 100 - fmt.Println("set:", a) - fmt.Println("get:", a[4]) + a[4] = 100 + fmt.Println("set:", a) + fmt.Println("get:", a[4]) - fmt.Println("len:", len(a)) + fmt.Println("len:", len(a)) - b := [5]int{1, 2, 3, 4, 5} - fmt.Println("dcl:", b) + b := [5]int{1, 2, 3, 4, 5} + fmt.Println("dcl:", b) - var twoD [2][3]int - for i := 0; i < 2; i++ { - for j := 0; j < 3; j++ { - twoD[i][j] = i + j - } - } - fmt.Println("2d: ", twoD) -} \ No newline at end of file + var twoD [2][3]int + for i := 0; i < 2; i++ { + for j := 0; j < 3; j++ { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/closures/closures.go b/src/github.com/statho7/GoByExample/BasicResources/closures/closures.go index 8c47fe6..f057a03 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/closures/closures.go +++ b/src/github.com/statho7/GoByExample/BasicResources/closures/closures.go @@ -3,21 +3,21 @@ package main import "fmt" func intSeq() func() int { - i := 0 - return func() int { - i++ - return i - } + i := 0 + return func() int { + i++ + return i + } } func main() { - nextInt := intSeq() + nextInt := intSeq() - fmt.Println(nextInt()) - fmt.Println(nextInt()) - fmt.Println(nextInt()) + fmt.Println(nextInt()) + fmt.Println(nextInt()) + fmt.Println(nextInt()) - newInts := intSeq() - fmt.Println(newInts()) -} \ No newline at end of file + newInts := intSeq() + fmt.Println(newInts()) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/collection_functions/collection_functions.go b/src/github.com/statho7/GoByExample/BasicResources/collection_functions/collection_functions.go index 565fcbb..375e0e2 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/collection_functions/collection_functions.go +++ b/src/github.com/statho7/GoByExample/BasicResources/collection_functions/collection_functions.go @@ -1,79 +1,79 @@ package main import ( - "fmt" - "strings" + "fmt" + "strings" ) func Index(vs []string, t string) int { - for i, v := range vs { - if v == t { - return i - } - } - return -1 + for i, v := range vs { + if v == t { + return i + } + } + return -1 } func Include(vs []string, t string) bool { - return Index(vs, t) >= 0 + return Index(vs, t) >= 0 } func Any(vs []string, f func(string) bool) bool { - for _, v := range vs { - if f(v) { - return true - } - } - return false + for _, v := range vs { + if f(v) { + return true + } + } + return false } func All(vs []string, f func(string) bool) bool { - for _, v := range vs { - if !f(v) { - return false - } - } - return true + for _, v := range vs { + if !f(v) { + return false + } + } + return true } func Filter(vs []string, f func(string) bool) []string { - vsf := make([]string, 0) - for _, v := range vs { - if f(v) { - vsf = append(vsf, v) - } - } - return vsf + vsf := make([]string, 0) + for _, v := range vs { + if f(v) { + vsf = append(vsf, v) + } + } + return vsf } func Map(vs []string, f func(string) string) []string { - vsm := make([]string, len(vs)) - for i, v := range vs { - vsm[i] = f(v) - } - return vsm + vsm := make([]string, len(vs)) + for i, v := range vs { + vsm[i] = f(v) + } + return vsm } func main() { - var strs = []string{"peach", "apple", "pear", "plum"} + var strs = []string{"peach", "apple", "pear", "plum"} - fmt.Println(Index(strs, "pear")) + fmt.Println(Index(strs, "pear")) - fmt.Println(Include(strs, "grape")) + fmt.Println(Include(strs, "grape")) - fmt.Println(Any(strs, func(v string) bool { - return strings.HasPrefix(v, "p") - })) + fmt.Println(Any(strs, func(v string) bool { + return strings.HasPrefix(v, "p") + })) - fmt.Println(All(strs, func(v string) bool { - return strings.HasPrefix(v, "p") - })) + fmt.Println(All(strs, func(v string) bool { + return strings.HasPrefix(v, "p") + })) - fmt.Println(Filter(strs, func(v string) bool { - return strings.Contains(v, "e") - })) + fmt.Println(Filter(strs, func(v string) bool { + return strings.Contains(v, "e") + })) - fmt.Println(Map(strs, strings.ToUpper)) + fmt.Println(Map(strs, strings.ToUpper)) -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/constants/constants.go b/src/github.com/statho7/GoByExample/BasicResources/constants/constants.go index c030546..251d5ce 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/constants/constants.go +++ b/src/github.com/statho7/GoByExample/BasicResources/constants/constants.go @@ -1,21 +1,21 @@ package main import ( - "fmt" - "math" + "fmt" + "math" ) const s string = "constant" func main() { - fmt.Println(s) + fmt.Println(s) - const n = 500000000 + const n = 500000000 - const d = 3e20 / n - fmt.Println(d) + const d = 3e20 / n + fmt.Println(d) - fmt.Println(int64(d)) + fmt.Println(int64(d)) - fmt.Println(math.Sin(n)) -} \ No newline at end of file + fmt.Println(math.Sin(n)) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/defer/defer.go b/src/github.com/statho7/GoByExample/BasicResources/defer/defer.go index d0b3fc9..977bbbe 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/defer/defer.go +++ b/src/github.com/statho7/GoByExample/BasicResources/defer/defer.go @@ -1,38 +1,38 @@ package main import ( - "fmt" - "os" + "fmt" + "os" ) func main() { - f := createFile("/tmp/defer.txt") - defer closeFile(f) - writeFile(f) + f := createFile("/tmp/defer.txt") + defer closeFile(f) + writeFile(f) } func createFile(p string) *os.File { - fmt.Println("creating") - f, err := os.Create(p) - if err != nil { - panic(err) - } - return f + fmt.Println("creating") + f, err := os.Create(p) + if err != nil { + panic(err) + } + return f } func writeFile(f *os.File) { - fmt.Println("writing") - fmt.Fprintln(f, "data") + fmt.Println("writing") + fmt.Fprintln(f, "data") } func closeFile(f *os.File) { - fmt.Println("closing") - err := f.Close() - - if err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - os.Exit(1) - } -} \ No newline at end of file + fmt.Println("closing") + err := f.Close() + + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/errors/errors.go b/src/github.com/statho7/GoByExample/BasicResources/errors/errors.go index 52c75da..899e81f 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/errors/errors.go +++ b/src/github.com/statho7/GoByExample/BasicResources/errors/errors.go @@ -1,57 +1,57 @@ package main import ( - "errors" - "fmt" + "errors" + "fmt" ) func f1(arg int) (int, error) { - if arg == 42 { + if arg == 42 { - return -1, errors.New("can't work with 42") + return -1, errors.New("can't work with 42") - } + } - return arg + 3, nil + return arg + 3, nil } type argError struct { - arg int - prob string + arg int + prob string } func (e *argError) Error() string { - return fmt.Sprintf("%d - %s", e.arg, e.prob) + return fmt.Sprintf("%d - %s", e.arg, e.prob) } func f2(arg int) (int, error) { - if arg == 42 { + if arg == 42 { - return -1, &argError{arg, "can't work with it"} - } - return arg + 3, nil + return -1, &argError{arg, "can't work with it"} + } + return arg + 3, nil } func main() { - for _, i := range []int{7, 42} { - if r, e := f1(i); e != nil { - fmt.Println("f1 failed:", e) - } else { - fmt.Println("f1 worked:", r) - } - } - for _, i := range []int{7, 42} { - if r, e := f2(i); e != nil { - fmt.Println("f2 failed:", e) - } else { - fmt.Println("f2 worked:", r) - } - } - - _, e := f2(42) - if ae, ok := e.(*argError); ok { - fmt.Println(ae.arg) - fmt.Println(ae.prob) - } -} \ No newline at end of file + for _, i := range []int{7, 42} { + if r, e := f1(i); e != nil { + fmt.Println("f1 failed:", e) + } else { + fmt.Println("f1 worked:", r) + } + } + for _, i := range []int{7, 42} { + if r, e := f2(i); e != nil { + fmt.Println("f2 failed:", e) + } else { + fmt.Println("f2 worked:", r) + } + } + + _, e := f2(42) + if ae, ok := e.(*argError); ok { + fmt.Println(ae.arg) + fmt.Println(ae.prob) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/for/for.go b/src/github.com/statho7/GoByExample/BasicResources/for/for.go index 4838ac6..c271793 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/for/for.go +++ b/src/github.com/statho7/GoByExample/BasicResources/for/for.go @@ -4,25 +4,25 @@ import "fmt" func main() { - i := 1 - for i <= 3 { - fmt.Println(i) - i = i + 1 - } + i := 1 + for i <= 3 { + fmt.Println(i) + i = i + 1 + } - for j := 7; j <= 9; j++ { - fmt.Println(j) - } + for j := 7; j <= 9; j++ { + fmt.Println(j) + } - for { - fmt.Println("loop") - break - } + for { + fmt.Println("loop") + break + } - for n := 0; n <= 5; n++ { - if n%2 == 0 { - continue - } - fmt.Println(n) - } -} \ No newline at end of file + for n := 0; n <= 5; n++ { + if n%2 == 0 { + continue + } + fmt.Println(n) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/functions/functions.go b/src/github.com/statho7/GoByExample/BasicResources/functions/functions.go index c5695ef..f6b1deb 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/functions/functions.go +++ b/src/github.com/statho7/GoByExample/BasicResources/functions/functions.go @@ -4,18 +4,18 @@ import "fmt" func plus(a int, b int) int { - return a + b + return a + b } func plusPlus(a, b, c int) int { - return a + b + c + return a + b + c } func main() { - res := plus(1, 2) - fmt.Println("1+2 =", res) + res := plus(1, 2) + fmt.Println("1+2 =", res) - res = plusPlus(1, 2, 3) - fmt.Println("1+2+3 =", res) -} \ No newline at end of file + res = plusPlus(1, 2, 3) + fmt.Println("1+2+3 =", res) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/if_else/if_else.go b/src/github.com/statho7/GoByExample/BasicResources/if_else/if_else.go index 1e801cf..1d2405c 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/if_else/if_else.go +++ b/src/github.com/statho7/GoByExample/BasicResources/if_else/if_else.go @@ -4,21 +4,21 @@ import "fmt" func main() { - if 7%2 == 0 { - fmt.Println("7 is even") - } else { - fmt.Println("7 is odd") - } + if 7%2 == 0 { + fmt.Println("7 is even") + } else { + fmt.Println("7 is odd") + } - if 8%4 == 0 { - fmt.Println("8 is divisible by 4") - } + if 8%4 == 0 { + fmt.Println("8 is divisible by 4") + } - if num := 9; num < 0 { - fmt.Println(num, "is negative") - } else if num < 10 { - fmt.Println(num, "has 1 digit") - } else { - fmt.Println(num, "has multiple digits") - } -} \ No newline at end of file + if num := 9; num < 0 { + fmt.Println(num, "is negative") + } else if num < 10 { + fmt.Println(num, "has 1 digit") + } else { + fmt.Println(num, "has multiple digits") + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/interfaces/interfaces.go b/src/github.com/statho7/GoByExample/BasicResources/interfaces/interfaces.go index 68bfc6f..2eafad3 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/interfaces/interfaces.go +++ b/src/github.com/statho7/GoByExample/BasicResources/interfaces/interfaces.go @@ -1,46 +1,46 @@ package main import ( - "fmt" - "math" + "fmt" + "math" ) type geometry interface { - area() float64 - perim() float64 + area() float64 + perim() float64 } type rect struct { - width, height float64 + width, height float64 } type circle struct { - radius float64 + radius float64 } func (r rect) area() float64 { - return r.width * r.height + return r.width * r.height } func (r rect) perim() float64 { - return 2*r.width + 2*r.height + return 2*r.width + 2*r.height } func (c circle) area() float64 { - return math.Pi * c.radius * c.radius + return math.Pi * c.radius * c.radius } func (c circle) perim() float64 { - return 2 * math.Pi * c.radius + return 2 * math.Pi * c.radius } func measure(g geometry) { - fmt.Println(g) - fmt.Println(g.area()) - fmt.Println(g.perim()) + fmt.Println(g) + fmt.Println(g.area()) + fmt.Println(g.perim()) } func main() { - r := rect{width: 3, height: 4} - c := circle{radius: 5} + r := rect{width: 3, height: 4} + c := circle{radius: 5} - measure(r) - measure(c) -} \ No newline at end of file + measure(r) + measure(c) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/line_filters/line_filters.go b/src/github.com/statho7/GoByExample/BasicResources/line_filters/line_filters.go index 5ecc8d1..69b9c67 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/line_filters/line_filters.go +++ b/src/github.com/statho7/GoByExample/BasicResources/line_filters/line_filters.go @@ -1,25 +1,25 @@ package main import ( - "bufio" - "fmt" - "os" - "strings" + "bufio" + "fmt" + "os" + "strings" ) func main() { - scanner := bufio.NewScanner(os.Stdin) + scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { + for scanner.Scan() { - ucl := strings.ToUpper(scanner.Text()) + ucl := strings.ToUpper(scanner.Text()) - fmt.Println(ucl) - } + fmt.Println(ucl) + } - if err := scanner.Err(); err != nil { - fmt.Fprintln(os.Stderr, "error:", err) - os.Exit(1) - } -} \ No newline at end of file + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/maps/maps.go b/src/github.com/statho7/GoByExample/BasicResources/maps/maps.go index daf39ce..5f64e69 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/maps/maps.go +++ b/src/github.com/statho7/GoByExample/BasicResources/maps/maps.go @@ -4,24 +4,24 @@ import "fmt" func main() { - m := make(map[string]int) + m := make(map[string]int) - m["k1"] = 7 - m["k2"] = 13 + m["k1"] = 7 + m["k2"] = 13 - fmt.Println("map:", m) + fmt.Println("map:", m) - v1 := m["k1"] - fmt.Println("v1: ", v1) + v1 := m["k1"] + fmt.Println("v1: ", v1) - fmt.Println("len:", len(m)) + fmt.Println("len:", len(m)) - delete(m, "k2") - fmt.Println("map:", m) + delete(m, "k2") + fmt.Println("map:", m) - _, prs := m["k2"] - fmt.Println("prs:", prs) + _, prs := m["k2"] + fmt.Println("prs:", prs) - n := map[string]int{"foo": 1, "bar": 2} - fmt.Println("map:", n) -} \ No newline at end of file + n := map[string]int{"foo": 1, "bar": 2} + fmt.Println("map:", n) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/methods/methods.go b/src/github.com/statho7/GoByExample/BasicResources/methods/methods.go index 8aaff98..b76681f 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/methods/methods.go +++ b/src/github.com/statho7/GoByExample/BasicResources/methods/methods.go @@ -3,24 +3,24 @@ package main import "fmt" type rect struct { - width, height int + width, height int } func (r *rect) area() int { - return r.width * r.height + return r.width * r.height } func (r rect) perim() int { - return 2*r.width + 2*r.height + return 2*r.width + 2*r.height } func main() { - r := rect{width: 10, height: 5} + r := rect{width: 10, height: 5} - fmt.Println("area: ", r.area()) - fmt.Println("perim:", r.perim()) + fmt.Println("area: ", r.area()) + fmt.Println("perim:", r.perim()) - rp := &r - fmt.Println("area: ", rp.area()) - fmt.Println("perim:", rp.perim()) -} \ No newline at end of file + rp := &r + fmt.Println("area: ", rp.area()) + fmt.Println("perim:", rp.perim()) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/multiple_return_values/multiple_return_values.go b/src/github.com/statho7/GoByExample/BasicResources/multiple_return_values/multiple_return_values.go index 0741927..6eafcd4 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/multiple_return_values/multiple_return_values.go +++ b/src/github.com/statho7/GoByExample/BasicResources/multiple_return_values/multiple_return_values.go @@ -3,15 +3,15 @@ package main import "fmt" func vals() (int, int) { - return 3, 7 + return 3, 7 } func main() { - a, b := vals() - fmt.Println(a) - fmt.Println(b) + a, b := vals() + fmt.Println(a) + fmt.Println(b) - _, c := vals() - fmt.Println(c) -} \ No newline at end of file + _, c := vals() + fmt.Println(c) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/panic/panic.go b/src/github.com/statho7/GoByExample/BasicResources/panic/panic.go index 544c617..b3270a5 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/panic/panic.go +++ b/src/github.com/statho7/GoByExample/BasicResources/panic/panic.go @@ -4,10 +4,10 @@ import "os" func main() { - panic("a problem") + panic("a problem") - _, err := os.Create("/tmp/file") - if err != nil { - panic(err) - } -} \ No newline at end of file + _, err := os.Create("/tmp/file") + if err != nil { + panic(err) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/pointers/pointers.go b/src/github.com/statho7/GoByExample/BasicResources/pointers/pointers.go index e33ffc1..e5f95f6 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/pointers/pointers.go +++ b/src/github.com/statho7/GoByExample/BasicResources/pointers/pointers.go @@ -3,22 +3,22 @@ package main import "fmt" func zeroval(ival int) { - ival = 0 + ival = 0 } func zeroptr(iptr *int) { - *iptr = 0 + *iptr = 0 } func main() { - i := 1 - fmt.Println("initial:", i) + i := 1 + fmt.Println("initial:", i) - zeroval(i) - fmt.Println("zeroval:", i) + zeroval(i) + fmt.Println("zeroval:", i) - zeroptr(&i) - fmt.Println("zeroptr:", i) + zeroptr(&i) + fmt.Println("zeroptr:", i) - fmt.Println("pointer:", &i) -} \ No newline at end of file + fmt.Println("pointer:", &i) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/range/range.go b/src/github.com/statho7/GoByExample/BasicResources/range/range.go index cfddb54..fd253c1 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/range/range.go +++ b/src/github.com/statho7/GoByExample/BasicResources/range/range.go @@ -4,29 +4,29 @@ import "fmt" func main() { - nums := []int{2, 3, 4} - sum := 0 - for _, num := range nums { - sum += num - } - fmt.Println("sum:", sum) + nums := []int{2, 3, 4} + sum := 0 + for _, num := range nums { + sum += num + } + fmt.Println("sum:", sum) - for i, num := range nums { - if num == 3 { - fmt.Println("index:", i) - } - } + for i, num := range nums { + if num == 3 { + fmt.Println("index:", i) + } + } - kvs := map[string]string{"a": "apple", "b": "banana"} - for k, v := range kvs { - fmt.Printf("%s -> %s\n", k, v) - } + kvs := map[string]string{"a": "apple", "b": "banana"} + for k, v := range kvs { + fmt.Printf("%s -> %s\n", k, v) + } - for k := range kvs { - fmt.Println("key:", k) - } + for k := range kvs { + fmt.Println("key:", k) + } - for i, c := range "go" { - fmt.Println(i, c) - } -} \ No newline at end of file + for i, c := range "go" { + fmt.Println(i, c) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/reading_files/reading_files.go b/src/github.com/statho7/GoByExample/BasicResources/reading_files/reading_files.go index 8c28be9..3cbd1e9 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/reading_files/reading_files.go +++ b/src/github.com/statho7/GoByExample/BasicResources/reading_files/reading_files.go @@ -1,54 +1,54 @@ package main import ( - "bufio" - "fmt" - "io" - "os" + "bufio" + "fmt" + "io" + "os" ) func check(e error) { - if e != nil { - panic(e) - } + if e != nil { + panic(e) + } } func main() { - dat, err := os.ReadFile("/tmp/dat") - check(err) - fmt.Print(string(dat)) - - f, err := os.Open("/tmp/dat") - check(err) - - b1 := make([]byte, 5) - n1, err := f.Read(b1) - check(err) - fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1])) - - o2, err := f.Seek(6, 0) - check(err) - b2 := make([]byte, 2) - n2, err := f.Read(b2) - check(err) - fmt.Printf("%d bytes @ %d: ", n2, o2) - fmt.Printf("%v\n", string(b2[:n2])) - - o3, err := f.Seek(6, 0) - check(err) - b3 := make([]byte, 2) - n3, err := io.ReadAtLeast(f, b3, 2) - check(err) - fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3)) - - _, err = f.Seek(0, 0) - check(err) - - r4 := bufio.NewReader(f) - b4, err := r4.Peek(5) - check(err) - fmt.Printf("5 bytes: %s\n", string(b4)) - - f.Close() -} \ No newline at end of file + dat, err := os.ReadFile("/tmp/dat") + check(err) + fmt.Print(string(dat)) + + f, err := os.Open("/tmp/dat") + check(err) + + b1 := make([]byte, 5) + n1, err := f.Read(b1) + check(err) + fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1])) + + o2, err := f.Seek(6, 0) + check(err) + b2 := make([]byte, 2) + n2, err := f.Read(b2) + check(err) + fmt.Printf("%d bytes @ %d: ", n2, o2) + fmt.Printf("%v\n", string(b2[:n2])) + + o3, err := f.Seek(6, 0) + check(err) + b3 := make([]byte, 2) + n3, err := io.ReadAtLeast(f, b3, 2) + check(err) + fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3)) + + _, err = f.Seek(0, 0) + check(err) + + r4 := bufio.NewReader(f) + b4, err := r4.Peek(5) + check(err) + fmt.Printf("5 bytes: %s\n", string(b4)) + + f.Close() +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/recursion/recursion.go b/src/github.com/statho7/GoByExample/BasicResources/recursion/recursion.go index 5ea552e..9454e58 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/recursion/recursion.go +++ b/src/github.com/statho7/GoByExample/BasicResources/recursion/recursion.go @@ -3,24 +3,24 @@ package main import "fmt" func fact(n int) int { - if n == 0 { - return 1 - } - return n * fact(n-1) + if n == 0 { + return 1 + } + return n * fact(n-1) } func main() { - fmt.Println(fact(7)) + fmt.Println(fact(7)) - var fib func(n int) int + var fib func(n int) int - fib = func(n int) int { - if n < 2 { - return n - } - return fib(n-1) + fib(n-2) + fib = func(n int) int { + if n < 2 { + return n + } + return fib(n-1) + fib(n-2) - } + } - fmt.Println(fib(7)) -} \ No newline at end of file + fmt.Println(fib(7)) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/slices/slices.go b/src/github.com/statho7/GoByExample/BasicResources/slices/slices.go index f97495d..44a9cd4 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/slices/slices.go +++ b/src/github.com/statho7/GoByExample/BasicResources/slices/slices.go @@ -4,44 +4,44 @@ import "fmt" func main() { - s := make([]string, 3) - fmt.Println("emp:", s) - - s[0] = "a" - s[1] = "b" - s[2] = "c" - fmt.Println("set:", s) - fmt.Println("get:", s[2]) - - fmt.Println("len:", len(s)) - - s = append(s, "d") - s = append(s, "e", "f") - fmt.Println("apd:", s) - - c := make([]string, len(s)) - copy(c, s) - fmt.Println("cpy:", c) - - l := s[2:5] - fmt.Println("sl1:", l) - - l = s[:5] - fmt.Println("sl2:", l) - - l = s[2:] - fmt.Println("sl3:", l) - - t := []string{"g", "h", "i"} - fmt.Println("dcl:", t) - - twoD := make([][]int, 3) - for i := 0; i < 3; i++ { - innerLen := i + 1 - twoD[i] = make([]int, innerLen) - for j := 0; j < innerLen; j++ { - twoD[i][j] = i + j - } - } - fmt.Println("2d: ", twoD) -} \ No newline at end of file + s := make([]string, 3) + fmt.Println("emp:", s) + + s[0] = "a" + s[1] = "b" + s[2] = "c" + fmt.Println("set:", s) + fmt.Println("get:", s[2]) + + fmt.Println("len:", len(s)) + + s = append(s, "d") + s = append(s, "e", "f") + fmt.Println("apd:", s) + + c := make([]string, len(s)) + copy(c, s) + fmt.Println("cpy:", c) + + l := s[2:5] + fmt.Println("sl1:", l) + + l = s[:5] + fmt.Println("sl2:", l) + + l = s[2:] + fmt.Println("sl3:", l) + + t := []string{"g", "h", "i"} + fmt.Println("dcl:", t) + + twoD := make([][]int, 3) + for i := 0; i < 3; i++ { + innerLen := i + 1 + twoD[i] = make([]int, innerLen) + for j := 0; j < innerLen; j++ { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/sorting/sorting.go b/src/github.com/statho7/GoByExample/BasicResources/sorting/sorting.go index 8f60e6b..a352632 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/sorting/sorting.go +++ b/src/github.com/statho7/GoByExample/BasicResources/sorting/sorting.go @@ -1,20 +1,20 @@ package main import ( - "fmt" - "sort" + "fmt" + "sort" ) func main() { - strs := []string{"c", "a", "b"} - sort.Strings(strs) - fmt.Println("Strings:", strs) + strs := []string{"c", "a", "b"} + sort.Strings(strs) + fmt.Println("Strings:", strs) - ints := []int{7, 2, 4} - sort.Ints(ints) - fmt.Println("Ints: ", ints) + ints := []int{7, 2, 4} + sort.Ints(ints) + fmt.Println("Ints: ", ints) - s := sort.IntsAreSorted(ints) - fmt.Println("Sorted: ", s) -} \ No newline at end of file + s := sort.IntsAreSorted(ints) + fmt.Println("Sorted: ", s) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/sorting_by_functions/sorting_by_functions.go b/src/github.com/statho7/GoByExample/BasicResources/sorting_by_functions/sorting_by_functions.go index e2b9d4d..2b1c695 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/sorting_by_functions/sorting_by_functions.go +++ b/src/github.com/statho7/GoByExample/BasicResources/sorting_by_functions/sorting_by_functions.go @@ -1,24 +1,24 @@ package main import ( - "fmt" - "sort" + "fmt" + "sort" ) type byLength []string func (s byLength) Len() int { - return len(s) + return len(s) } func (s byLength) Swap(i, j int) { - s[i], s[j] = s[j], s[i] + s[i], s[j] = s[j], s[i] } func (s byLength) Less(i, j int) bool { - return len(s[i]) < len(s[j]) + return len(s[i]) < len(s[j]) } func main() { - fruits := []string{"peach", "banana", "kiwi"} - sort.Sort(byLength(fruits)) - fmt.Println(fruits) -} \ No newline at end of file + fruits := []string{"peach", "banana", "kiwi"} + sort.Sort(byLength(fruits)) + fmt.Println(fruits) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/structs/structs.go b/src/github.com/statho7/GoByExample/BasicResources/structs/structs.go index 97f18ad..193ec9a 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/structs/structs.go +++ b/src/github.com/statho7/GoByExample/BasicResources/structs/structs.go @@ -3,35 +3,35 @@ package main import "fmt" type person struct { - name string - age int + name string + age int } func newPerson(name string) *person { - p := person{name: name} - p.age = 42 - return &p + p := person{name: name} + p.age = 42 + return &p } func main() { - fmt.Println(person{"Bob", 20}) + fmt.Println(person{"Bob", 20}) - fmt.Println(person{name: "Alice", age: 30}) + fmt.Println(person{name: "Alice", age: 30}) - fmt.Println(person{name: "Fred"}) + fmt.Println(person{name: "Fred"}) - fmt.Println(&person{name: "Ann", age: 40}) + fmt.Println(&person{name: "Ann", age: 40}) - fmt.Println(newPerson("Jon")) + fmt.Println(newPerson("Jon")) - s := person{name: "Sean", age: 50} - fmt.Println(s.name) + s := person{name: "Sean", age: 50} + fmt.Println(s.name) - sp := &s - fmt.Println(sp.age) + sp := &s + fmt.Println(sp.age) - sp.age = 51 - fmt.Println(sp.age) -} \ No newline at end of file + sp.age = 51 + fmt.Println(sp.age) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/switch/switch.go b/src/github.com/statho7/GoByExample/BasicResources/switch/switch.go index 83b0083..545f1a6 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/switch/switch.go +++ b/src/github.com/statho7/GoByExample/BasicResources/switch/switch.go @@ -1,49 +1,49 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - i := 2 - fmt.Print("Write ", i, " as ") - switch i { - case 1: - fmt.Println("one") - case 2: - fmt.Println("two") - case 3: - fmt.Println("three") - } + i := 2 + fmt.Print("Write ", i, " as ") + switch i { + case 1: + fmt.Println("one") + case 2: + fmt.Println("two") + case 3: + fmt.Println("three") + } - switch time.Now().Weekday() { - case time.Saturday, time.Sunday: - fmt.Println("It's the weekend") - default: - fmt.Println("It's a weekday") - } + switch time.Now().Weekday() { + case time.Saturday, time.Sunday: + fmt.Println("It's the weekend") + default: + fmt.Println("It's a weekday") + } - t := time.Now() - switch { - case t.Hour() < 12: - fmt.Println("It's before noon") - default: - fmt.Println("It's after noon") - } + t := time.Now() + switch { + case t.Hour() < 12: + fmt.Println("It's before noon") + default: + fmt.Println("It's after noon") + } - whatAmI := func(i interface{}) { - switch t := i.(type) { - case bool: - fmt.Println("I'm a bool") - case int: - fmt.Println("I'm an int") - default: - fmt.Printf("Don't know type %T\n", t) - } - } - whatAmI(true) - whatAmI(1) - whatAmI("hey") -} \ No newline at end of file + whatAmI := func(i interface{}) { + switch t := i.(type) { + case bool: + fmt.Println("I'm a bool") + case int: + fmt.Println("I'm an int") + default: + fmt.Printf("Don't know type %T\n", t) + } + } + whatAmI(true) + whatAmI(1) + whatAmI("hey") +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/testing/testing.go b/src/github.com/statho7/GoByExample/BasicResources/testing/testing.go index 8461cf1..57b786d 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/testing/testing.go +++ b/src/github.com/statho7/GoByExample/BasicResources/testing/testing.go @@ -1,51 +1,51 @@ package main import ( - "fmt" - "testing" + "fmt" + "testing" ) func IntMin(a, b int) int { - if a < b { - return a - } - return b + if a < b { + return a + } + return b } func TestIntMinBasic(t *testing.T) { - ans := IntMin(2, -2) - if ans != -2 { + ans := IntMin(2, -2) + if ans != -2 { - t.Errorf("IntMin(2, -2) = %d; want -2", ans) - } + t.Errorf("IntMin(2, -2) = %d; want -2", ans) + } } func TestIntMinTableDriven(t *testing.T) { - var tests = []struct { - a, b int - want int - }{ - {0, 1, 0}, - {1, 0, 0}, - {2, -2, -2}, - {0, -1, -1}, - {-1, 0, -1}, - } - - for _, tt := range tests { - - testname := fmt.Sprintf("%d,%d", tt.a, tt.b) - t.Run(testname, func(t *testing.T) { - ans := IntMin(tt.a, tt.b) - if ans != tt.want { - t.Errorf("got %d, want %d", ans, tt.want) - } - }) - } + var tests = []struct { + a, b int + want int + }{ + {0, 1, 0}, + {1, 0, 0}, + {2, -2, -2}, + {0, -1, -1}, + {-1, 0, -1}, + } + + for _, tt := range tests { + + testname := fmt.Sprintf("%d,%d", tt.a, tt.b) + t.Run(testname, func(t *testing.T) { + ans := IntMin(tt.a, tt.b) + if ans != tt.want { + t.Errorf("got %d, want %d", ans, tt.want) + } + }) + } } func BenchmarkIntMin(b *testing.B) { - for i := 0; i < b.N; i++ { - IntMin(1, 2) - } -} \ No newline at end of file + for i := 0; i < b.N; i++ { + IntMin(1, 2) + } +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/values/values.go b/src/github.com/statho7/GoByExample/BasicResources/values/values.go index f564f3b..2da0a0c 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/values/values.go +++ b/src/github.com/statho7/GoByExample/BasicResources/values/values.go @@ -4,12 +4,12 @@ import "fmt" func main() { - fmt.Println("go" + "lang") + fmt.Println("go" + "lang") - fmt.Println("1+1 =", 1+1) - fmt.Println("7.0/3.0 =", 7.0/3.0) + fmt.Println("1+1 =", 1+1) + fmt.Println("7.0/3.0 =", 7.0/3.0) - fmt.Println(true && false) - fmt.Println(true || false) - fmt.Println(!true) -} \ No newline at end of file + fmt.Println(true && false) + fmt.Println(true || false) + fmt.Println(!true) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/variables/variables.go b/src/github.com/statho7/GoByExample/BasicResources/variables/variables.go index 3ad0ed6..18d1535 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/variables/variables.go +++ b/src/github.com/statho7/GoByExample/BasicResources/variables/variables.go @@ -4,18 +4,18 @@ import "fmt" func main() { - var a = "initial" - fmt.Println(a) + var a = "initial" + fmt.Println(a) - var b, c int = 1, 2 - fmt.Println(b, c) + var b, c int = 1, 2 + fmt.Println(b, c) - var d = true - fmt.Println(d) + var d = true + fmt.Println(d) - var e int - fmt.Println(e) + var e int + fmt.Println(e) - f := "apple" - fmt.Println(f) -} \ No newline at end of file + f := "apple" + fmt.Println(f) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/variadic_functions/variadic_functions.go b/src/github.com/statho7/GoByExample/BasicResources/variadic_functions/variadic_functions.go index 0c8c5b2..663216d 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/variadic_functions/variadic_functions.go +++ b/src/github.com/statho7/GoByExample/BasicResources/variadic_functions/variadic_functions.go @@ -3,19 +3,19 @@ package main import "fmt" func sum(nums ...int) { - fmt.Print(nums, " ") - total := 0 - for _, num := range nums { - total += num - } - fmt.Println(total) + fmt.Print(nums, " ") + total := 0 + for _, num := range nums { + total += num + } + fmt.Println(total) } func main() { - sum(1, 2) - sum(1, 2, 3) + sum(1, 2) + sum(1, 2, 3) - nums := []int{1, 2, 3, 4} - sum(nums...) -} \ No newline at end of file + nums := []int{1, 2, 3, 4} + sum(nums...) +} diff --git a/src/github.com/statho7/GoByExample/BasicResources/writing_files/writing_files.go b/src/github.com/statho7/GoByExample/BasicResources/writing_files/writing_files.go index fb74079..5a25dcf 100644 --- a/src/github.com/statho7/GoByExample/BasicResources/writing_files/writing_files.go +++ b/src/github.com/statho7/GoByExample/BasicResources/writing_files/writing_files.go @@ -1,44 +1,44 @@ package main import ( - "bufio" - "fmt" - "os" + "bufio" + "fmt" + "os" ) func check(e error) { - if e != nil { - panic(e) - } + if e != nil { + panic(e) + } } func main() { - d1 := []byte("hello\ngo\n") - err := os.WriteFile("/tmp/dat1", d1, 0644) - check(err) + d1 := []byte("hello\ngo\n") + err := os.WriteFile("/tmp/dat1", d1, 0644) + check(err) - f, err := os.Create("/tmp/dat2") - check(err) + f, err := os.Create("/tmp/dat2") + check(err) - defer f.Close() + defer f.Close() - d2 := []byte{115, 111, 109, 101, 10} - n2, err := f.Write(d2) - check(err) - fmt.Printf("wrote %d bytes\n", n2) + d2 := []byte{115, 111, 109, 101, 10} + n2, err := f.Write(d2) + check(err) + fmt.Printf("wrote %d bytes\n", n2) - n3, err := f.WriteString("writes\n") - check(err) - fmt.Printf("wrote %d bytes\n", n3) + n3, err := f.WriteString("writes\n") + check(err) + fmt.Printf("wrote %d bytes\n", n3) - f.Sync() + f.Sync() - w := bufio.NewWriter(f) - n4, err := w.WriteString("buffered\n") - check(err) - fmt.Printf("wrote %d bytes\n", n4) + w := bufio.NewWriter(f) + n4, err := w.WriteString("buffered\n") + check(err) + fmt.Printf("wrote %d bytes\n", n4) - w.Flush() + w.Flush() -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoByExample/Extra/base64_encoding/base64_encoding.go b/src/github.com/statho7/GoByExample/Extra/base64_encoding/base64_encoding.go index 66152c7..b23c3fc 100644 --- a/src/github.com/statho7/GoByExample/Extra/base64_encoding/base64_encoding.go +++ b/src/github.com/statho7/GoByExample/Extra/base64_encoding/base64_encoding.go @@ -1,23 +1,23 @@ package main import ( - b64 "encoding/base64" - "fmt" + b64 "encoding/base64" + "fmt" ) func main() { - data := "abc123!?$*&()'-=@~" + data := "abc123!?$*&()'-=@~" - sEnc := b64.StdEncoding.EncodeToString([]byte(data)) - fmt.Println(sEnc) + sEnc := b64.StdEncoding.EncodeToString([]byte(data)) + fmt.Println(sEnc) - sDec, _ := b64.StdEncoding.DecodeString(sEnc) - fmt.Println(string(sDec)) - fmt.Println() + sDec, _ := b64.StdEncoding.DecodeString(sEnc) + fmt.Println(string(sDec)) + fmt.Println() - uEnc := b64.URLEncoding.EncodeToString([]byte(data)) - fmt.Println(uEnc) - uDec, _ := b64.URLEncoding.DecodeString(uEnc) - fmt.Println(string(uDec)) -} \ No newline at end of file + uEnc := b64.URLEncoding.EncodeToString([]byte(data)) + fmt.Println(uEnc) + uDec, _ := b64.URLEncoding.DecodeString(uEnc) + fmt.Println(string(uDec)) +} diff --git a/src/github.com/statho7/GoByExample/Extra/directories/directories.go b/src/github.com/statho7/GoByExample/Extra/directories/directories.go index 31b39c2..235e300 100644 --- a/src/github.com/statho7/GoByExample/Extra/directories/directories.go +++ b/src/github.com/statho7/GoByExample/Extra/directories/directories.go @@ -1,68 +1,68 @@ package main import ( - "fmt" - "os" - "path/filepath" + "fmt" + "os" + "path/filepath" ) func check(e error) { - if e != nil { - panic(e) - } + if e != nil { + panic(e) + } } func main() { - err := os.Mkdir("subdir", 0755) - check(err) + err := os.Mkdir("subdir", 0755) + check(err) - defer os.RemoveAll("subdir") + defer os.RemoveAll("subdir") - createEmptyFile := func(name string) { - d := []byte("") - check(os.WriteFile(name, d, 0644)) - } + createEmptyFile := func(name string) { + d := []byte("") + check(os.WriteFile(name, d, 0644)) + } - createEmptyFile("subdir/file1") + createEmptyFile("subdir/file1") - err = os.MkdirAll("subdir/parent/child", 0755) - check(err) + err = os.MkdirAll("subdir/parent/child", 0755) + check(err) - createEmptyFile("subdir/parent/file2") - createEmptyFile("subdir/parent/file3") - createEmptyFile("subdir/parent/child/file4") + createEmptyFile("subdir/parent/file2") + createEmptyFile("subdir/parent/file3") + createEmptyFile("subdir/parent/child/file4") - c, err := os.ReadDir("subdir/parent") - check(err) + c, err := os.ReadDir("subdir/parent") + check(err) - fmt.Println("Listing subdir/parent") - for _, entry := range c { - fmt.Println(" ", entry.Name(), entry.IsDir()) - } + fmt.Println("Listing subdir/parent") + for _, entry := range c { + fmt.Println(" ", entry.Name(), entry.IsDir()) + } - err = os.Chdir("subdir/parent/child") - check(err) + err = os.Chdir("subdir/parent/child") + check(err) - c, err = os.ReadDir(".") - check(err) + c, err = os.ReadDir(".") + check(err) - fmt.Println("Listing subdir/parent/child") - for _, entry := range c { - fmt.Println(" ", entry.Name(), entry.IsDir()) - } + fmt.Println("Listing subdir/parent/child") + for _, entry := range c { + fmt.Println(" ", entry.Name(), entry.IsDir()) + } - err = os.Chdir("../../..") - check(err) + err = os.Chdir("../../..") + check(err) - fmt.Println("Visiting subdir") - err = filepath.Walk("subdir", visit) + fmt.Println("Visiting subdir") + err = filepath.Walk("subdir", visit) } func visit(p string, info os.FileInfo, err error) error { - if err != nil { - return err - } - fmt.Println(" ", p, info.IsDir()) - return nil -} \ No newline at end of file + if err != nil { + return err + } + fmt.Println(" ", p, info.IsDir()) + return nil +} diff --git a/src/github.com/statho7/GoByExample/Extra/epoch/epoch.go b/src/github.com/statho7/GoByExample/Extra/epoch/epoch.go index 4d5d459..bae1b0d 100644 --- a/src/github.com/statho7/GoByExample/Extra/epoch/epoch.go +++ b/src/github.com/statho7/GoByExample/Extra/epoch/epoch.go @@ -1,19 +1,19 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - now := time.Now() - fmt.Println(now) + now := time.Now() + fmt.Println(now) - fmt.Println(now.Unix()) - fmt.Println(now.UnixMilli()) - fmt.Println(now.UnixNano()) + fmt.Println(now.Unix()) + fmt.Println(now.UnixMilli()) + fmt.Println(now.UnixNano()) - fmt.Println(time.Unix(now.Unix(), 0)) - fmt.Println(time.Unix(0, now.UnixNano())) -} \ No newline at end of file + fmt.Println(time.Unix(now.Unix(), 0)) + fmt.Println(time.Unix(0, now.UnixNano())) +} diff --git a/src/github.com/statho7/GoByExample/Extra/file_paths/file_paths.go b/src/github.com/statho7/GoByExample/Extra/file_paths/file_paths.go index 718f130..04bf4f7 100644 --- a/src/github.com/statho7/GoByExample/Extra/file_paths/file_paths.go +++ b/src/github.com/statho7/GoByExample/Extra/file_paths/file_paths.go @@ -1,41 +1,41 @@ package main import ( - "fmt" - "path/filepath" - "strings" + "fmt" + "path/filepath" + "strings" ) func main() { - p := filepath.Join("dir1", "dir2", "filename") - fmt.Println("p:", p) + p := filepath.Join("dir1", "dir2", "filename") + fmt.Println("p:", p) - fmt.Println(filepath.Join("dir1//", "filename")) - fmt.Println(filepath.Join("dir1/../dir1", "filename")) + fmt.Println(filepath.Join("dir1//", "filename")) + fmt.Println(filepath.Join("dir1/../dir1", "filename")) - fmt.Println("Dir(p):", filepath.Dir(p)) - fmt.Println("Base(p):", filepath.Base(p)) + fmt.Println("Dir(p):", filepath.Dir(p)) + fmt.Println("Base(p):", filepath.Base(p)) - fmt.Println(filepath.IsAbs("dir/file")) - fmt.Println(filepath.IsAbs("/dir/file")) + fmt.Println(filepath.IsAbs("dir/file")) + fmt.Println(filepath.IsAbs("/dir/file")) - filename := "config.json" + filename := "config.json" - ext := filepath.Ext(filename) - fmt.Println(ext) + ext := filepath.Ext(filename) + fmt.Println(ext) - fmt.Println(strings.TrimSuffix(filename, ext)) + fmt.Println(strings.TrimSuffix(filename, ext)) - rel, err := filepath.Rel("a/b", "a/b/t/file") - if err != nil { - panic(err) - } - fmt.Println(rel) + rel, err := filepath.Rel("a/b", "a/b/t/file") + if err != nil { + panic(err) + } + fmt.Println(rel) - rel, err = filepath.Rel("a/b", "a/c/t/file") - if err != nil { - panic(err) - } - fmt.Println(rel) -} \ No newline at end of file + rel, err = filepath.Rel("a/b", "a/c/t/file") + if err != nil { + panic(err) + } + fmt.Println(rel) +} diff --git a/src/github.com/statho7/GoByExample/Extra/regular_expressions/regular_expressions.go b/src/github.com/statho7/GoByExample/Extra/regular_expressions/regular_expressions.go index 7c85252..6af7765 100644 --- a/src/github.com/statho7/GoByExample/Extra/regular_expressions/regular_expressions.go +++ b/src/github.com/statho7/GoByExample/Extra/regular_expressions/regular_expressions.go @@ -1,43 +1,43 @@ package main import ( - "bytes" - "fmt" - "regexp" + "bytes" + "fmt" + "regexp" ) func main() { - match, _ := regexp.MatchString("p([a-z]+)ch", "peach") - fmt.Println(match) + match, _ := regexp.MatchString("p([a-z]+)ch", "peach") + fmt.Println(match) - r, _ := regexp.Compile("p([a-z]+)ch") + r, _ := regexp.Compile("p([a-z]+)ch") - fmt.Println(r.MatchString("peach")) + fmt.Println(r.MatchString("peach")) - fmt.Println(r.FindString("peach punch")) + fmt.Println(r.FindString("peach punch")) - fmt.Println("idx:", r.FindStringIndex("peach punch")) + fmt.Println("idx:", r.FindStringIndex("peach punch")) - fmt.Println(r.FindStringSubmatch("peach punch")) + fmt.Println(r.FindStringSubmatch("peach punch")) - fmt.Println(r.FindStringSubmatchIndex("peach punch")) + fmt.Println(r.FindStringSubmatchIndex("peach punch")) - fmt.Println(r.FindAllString("peach punch pinch", -1)) + fmt.Println(r.FindAllString("peach punch pinch", -1)) - fmt.Println("all:", r.FindAllStringSubmatchIndex( - "peach punch pinch", -1)) + fmt.Println("all:", r.FindAllStringSubmatchIndex( + "peach punch pinch", -1)) - fmt.Println(r.FindAllString("peach punch pinch", 2)) + fmt.Println(r.FindAllString("peach punch pinch", 2)) - fmt.Println(r.Match([]byte("peach"))) + fmt.Println(r.Match([]byte("peach"))) - r = regexp.MustCompile("p([a-z]+)ch") - fmt.Println("regexp:", r) + r = regexp.MustCompile("p([a-z]+)ch") + fmt.Println("regexp:", r) - fmt.Println(r.ReplaceAllString("a peach", "")) + fmt.Println(r.ReplaceAllString("a peach", "")) - in := []byte("a peach") - out := r.ReplaceAllFunc(in, bytes.ToUpper) - fmt.Println(string(out)) -} \ No newline at end of file + in := []byte("a peach") + out := r.ReplaceAllFunc(in, bytes.ToUpper) + fmt.Println(string(out)) +} diff --git a/src/github.com/statho7/GoByExample/Extra/sha1_hashes/sha1_hashes.go b/src/github.com/statho7/GoByExample/Extra/sha1_hashes/sha1_hashes.go index 2a39e1a..aa48efa 100644 --- a/src/github.com/statho7/GoByExample/Extra/sha1_hashes/sha1_hashes.go +++ b/src/github.com/statho7/GoByExample/Extra/sha1_hashes/sha1_hashes.go @@ -1,19 +1,19 @@ package main import ( - "crypto/sha1" - "fmt" + "crypto/sha1" + "fmt" ) func main() { - s := "sha1 this string" + s := "sha1 this string" - h := sha1.New() + h := sha1.New() - h.Write([]byte(s)) + h.Write([]byte(s)) - bs := h.Sum(nil) + bs := h.Sum(nil) - fmt.Println(s) - fmt.Printf("%x\n", bs) -} \ No newline at end of file + fmt.Println(s) + fmt.Printf("%x\n", bs) +} diff --git a/src/github.com/statho7/GoByExample/Extra/string_formatting/string_formatting.go b/src/github.com/statho7/GoByExample/Extra/string_formatting/string_formatting.go index 52ae4e6..2d2f1c8 100644 --- a/src/github.com/statho7/GoByExample/Extra/string_formatting/string_formatting.go +++ b/src/github.com/statho7/GoByExample/Extra/string_formatting/string_formatting.go @@ -1,60 +1,60 @@ package main import ( - "fmt" - "os" + "fmt" + "os" ) type point struct { - x, y int + x, y int } func main() { - p := point{1, 2} - fmt.Printf("struct1: %v\n", p) + p := point{1, 2} + fmt.Printf("struct1: %v\n", p) - fmt.Printf("struct2: %+v\n", p) + fmt.Printf("struct2: %+v\n", p) - fmt.Printf("struct3: %#v\n", p) + fmt.Printf("struct3: %#v\n", p) - fmt.Printf("type: %T\n", p) + fmt.Printf("type: %T\n", p) - fmt.Printf("bool: %t\n", true) + fmt.Printf("bool: %t\n", true) - fmt.Printf("int: %d\n", 123) + fmt.Printf("int: %d\n", 123) - fmt.Printf("bin: %b\n", 14) + fmt.Printf("bin: %b\n", 14) - fmt.Printf("char: %c\n", 33) + fmt.Printf("char: %c\n", 33) - fmt.Printf("hex: %x\n", 456) + fmt.Printf("hex: %x\n", 456) - fmt.Printf("float1: %f\n", 78.9) + fmt.Printf("float1: %f\n", 78.9) - fmt.Printf("float2: %e\n", 123400000.0) - fmt.Printf("float3: %E\n", 123400000.0) + fmt.Printf("float2: %e\n", 123400000.0) + fmt.Printf("float3: %E\n", 123400000.0) - fmt.Printf("str1: %s\n", "\"string\"") + fmt.Printf("str1: %s\n", "\"string\"") - fmt.Printf("str2: %q\n", "\"string\"") + fmt.Printf("str2: %q\n", "\"string\"") - fmt.Printf("str3: %x\n", "hex this") + fmt.Printf("str3: %x\n", "hex this") - fmt.Printf("pointer: %p\n", &p) + fmt.Printf("pointer: %p\n", &p) - fmt.Printf("width1: |%6d|%6d|\n", 12, 345) + fmt.Printf("width1: |%6d|%6d|\n", 12, 345) - fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45) + fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45) - fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45) + fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45) - fmt.Printf("width4: |%6s|%6s|\n", "foo", "b") + fmt.Printf("width4: |%6s|%6s|\n", "foo", "b") - fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b") + fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b") - s := fmt.Sprintf("sprintf: a %s", "string") - fmt.Println(s) + s := fmt.Sprintf("sprintf: a %s", "string") + fmt.Println(s) - fmt.Fprintf(os.Stderr, "io: an %s\n", "error") -} \ No newline at end of file + fmt.Fprintf(os.Stderr, "io: an %s\n", "error") +} diff --git a/src/github.com/statho7/GoByExample/Extra/string_functions/string_functions.go b/src/github.com/statho7/GoByExample/Extra/string_functions/string_functions.go index e916983..0ac617d 100644 --- a/src/github.com/statho7/GoByExample/Extra/string_functions/string_functions.go +++ b/src/github.com/statho7/GoByExample/Extra/string_functions/string_functions.go @@ -1,28 +1,28 @@ package main import ( - "fmt" - s "strings" + "fmt" + s "strings" ) var p = fmt.Println func main() { - p("Contains: ", s.Contains("test", "es")) - p("Count: ", s.Count("test", "t")) - p("HasPrefix: ", s.HasPrefix("test", "te")) - p("HasSuffix: ", s.HasSuffix("test", "st")) - p("Index: ", s.Index("test", "e")) - p("Join: ", s.Join([]string{"a", "b"}, "-")) - p("Repeat: ", s.Repeat("a", 5)) - p("Replace: ", s.Replace("foo", "o", "0", -1)) - p("Replace: ", s.Replace("foo", "o", "0", 1)) - p("Split: ", s.Split("a-b-c-d-e", "-")) - p("ToLower: ", s.ToLower("TEST")) - p("ToUpper: ", s.ToUpper("test")) - p() + p("Contains: ", s.Contains("test", "es")) + p("Count: ", s.Count("test", "t")) + p("HasPrefix: ", s.HasPrefix("test", "te")) + p("HasSuffix: ", s.HasSuffix("test", "st")) + p("Index: ", s.Index("test", "e")) + p("Join: ", s.Join([]string{"a", "b"}, "-")) + p("Repeat: ", s.Repeat("a", 5)) + p("Replace: ", s.Replace("foo", "o", "0", -1)) + p("Replace: ", s.Replace("foo", "o", "0", 1)) + p("Split: ", s.Split("a-b-c-d-e", "-")) + p("ToLower: ", s.ToLower("TEST")) + p("ToUpper: ", s.ToUpper("test")) + p() - p("Len: ", len("hello")) - p("Char:", "hello"[1]) -} \ No newline at end of file + p("Len: ", len("hello")) + p("Char:", "hello"[1]) +} diff --git a/src/github.com/statho7/GoByExample/Extra/temporary_files_and_directories/temporary_files_and_directories.go b/src/github.com/statho7/GoByExample/Extra/temporary_files_and_directories/temporary_files_and_directories.go index 54a7ff5..2122891 100644 --- a/src/github.com/statho7/GoByExample/Extra/temporary_files_and_directories/temporary_files_and_directories.go +++ b/src/github.com/statho7/GoByExample/Extra/temporary_files_and_directories/temporary_files_and_directories.go @@ -1,36 +1,36 @@ package main import ( - "fmt" - "os" - "path/filepath" + "fmt" + "os" + "path/filepath" ) func check(e error) { - if e != nil { - panic(e) - } + if e != nil { + panic(e) + } } func main() { - f, err := os.CreateTemp("", "sample") - check(err) + f, err := os.CreateTemp("", "sample") + check(err) - fmt.Println("Temp file name:", f.Name()) + fmt.Println("Temp file name:", f.Name()) - defer os.Remove(f.Name()) + defer os.Remove(f.Name()) - _, err = f.Write([]byte{1, 2, 3, 4}) - check(err) + _, err = f.Write([]byte{1, 2, 3, 4}) + check(err) - dname, err := os.MkdirTemp("", "sampledir") - check(err) - fmt.Println("Temp dir name:", dname) + dname, err := os.MkdirTemp("", "sampledir") + check(err) + fmt.Println("Temp dir name:", dname) - defer os.RemoveAll(dname) + defer os.RemoveAll(dname) - fname := filepath.Join(dname, "file1") - err = os.WriteFile(fname, []byte{1, 2}, 0666) - check(err) -} \ No newline at end of file + fname := filepath.Join(dname, "file1") + err = os.WriteFile(fname, []byte{1, 2}, 0666) + check(err) +} diff --git a/src/github.com/statho7/GoByExample/Extra/time/time.go b/src/github.com/statho7/GoByExample/Extra/time/time.go index 6a521f4..b3cbefd 100644 --- a/src/github.com/statho7/GoByExample/Extra/time/time.go +++ b/src/github.com/statho7/GoByExample/Extra/time/time.go @@ -1,43 +1,43 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - p := fmt.Println + p := fmt.Println - now := time.Now() - p(now) + now := time.Now() + p(now) - then := time.Date( - 2009, 11, 17, 20, 34, 58, 651387237, time.UTC) - p(then) + then := time.Date( + 2009, 11, 17, 20, 34, 58, 651387237, time.UTC) + p(then) - p(then.Year()) - p(then.Month()) - p(then.Day()) - p(then.Hour()) - p(then.Minute()) - p(then.Second()) - p(then.Nanosecond()) - p(then.Location()) + p(then.Year()) + p(then.Month()) + p(then.Day()) + p(then.Hour()) + p(then.Minute()) + p(then.Second()) + p(then.Nanosecond()) + p(then.Location()) - p(then.Weekday()) + p(then.Weekday()) - p(then.Before(now)) - p(then.After(now)) - p(then.Equal(now)) + p(then.Before(now)) + p(then.After(now)) + p(then.Equal(now)) - diff := now.Sub(then) - p(diff) + diff := now.Sub(then) + p(diff) - p(diff.Hours()) - p(diff.Minutes()) - p(diff.Seconds()) - p(diff.Nanoseconds()) + p(diff.Hours()) + p(diff.Minutes()) + p(diff.Seconds()) + p(diff.Nanoseconds()) - p(then.Add(diff)) - p(then.Add(-diff)) -} \ No newline at end of file + p(then.Add(diff)) + p(then.Add(-diff)) +} diff --git a/src/github.com/statho7/GoByExample/Extra/time_formatting/time_formatting.go b/src/github.com/statho7/GoByExample/Extra/time_formatting/time_formatting.go index ac1cee7..0ec16a2 100644 --- a/src/github.com/statho7/GoByExample/Extra/time_formatting/time_formatting.go +++ b/src/github.com/statho7/GoByExample/Extra/time_formatting/time_formatting.go @@ -1,33 +1,33 @@ package main import ( - "fmt" - "time" + "fmt" + "time" ) func main() { - p := fmt.Println + p := fmt.Println - t := time.Now() - p(t.Format(time.RFC3339)) + t := time.Now() + p(t.Format(time.RFC3339)) - t1, e := time.Parse( - time.RFC3339, - "2012-11-01T22:08:41+00:00") - p(t1) + t1, e := time.Parse( + time.RFC3339, + "2012-11-01T22:08:41+00:00") + p(t1) - p(t.Format("3:04PM")) - p(t.Format("Mon Jan _2 15:04:05 2006")) - p(t.Format("2006-01-02T15:04:05.999999-07:00")) - form := "3 04 PM" - t2, e := time.Parse(form, "8 41 PM") - p(t2) + p(t.Format("3:04PM")) + p(t.Format("Mon Jan _2 15:04:05 2006")) + p(t.Format("2006-01-02T15:04:05.999999-07:00")) + form := "3 04 PM" + t2, e := time.Parse(form, "8 41 PM") + p(t2) - fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", - t.Year(), t.Month(), t.Day(), - t.Hour(), t.Minute(), t.Second()) + fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", + t.Year(), t.Month(), t.Day(), + t.Hour(), t.Minute(), t.Second()) - ansic := "Mon Jan _2 15:04:05 2006" - _, e = time.Parse(ansic, "8:41PM") - p(e) -} \ No newline at end of file + ansic := "Mon Jan _2 15:04:05 2006" + _, e = time.Parse(ansic, "8:41PM") + p(e) +} diff --git a/src/github.com/statho7/GoTutorial/Channels/channels.go b/src/github.com/statho7/GoTutorial/Channels/channels.go index 4eecf69..ab21939 100644 --- a/src/github.com/statho7/GoTutorial/Channels/channels.go +++ b/src/github.com/statho7/GoTutorial/Channels/channels.go @@ -111,12 +111,11 @@ package main // close(logCh) // }() // logCh <- logEntry{time.Now(), logInfo, "App is starting"} - + // logCh <- logEntry{time.Now(), logInfo, "App is shutting down"} // time.Sleep(100 * time.Millisecond) // } - // func logger() { // for entry := range logCh { // fmt.Printf("%v - [%v]%v\n", entry.time.Format("2006-01-02T15:04:05"), entry.severity, entry.message) @@ -129,15 +128,15 @@ import ( ) const ( - logInfo = "INFO" + logInfo = "INFO" logWarning = "WARNING" - logError = "ERROR" + logError = "ERROR" ) type logEntry struct { - time time.Time + time time.Time severity string - message string + message string } var logCh = make(chan logEntry, 50) @@ -145,26 +144,25 @@ var doneCh = make(chan struct{}) func main() { go logger() - defer func(){ + defer func() { close(logCh) }() logCh <- logEntry{time.Now(), logInfo, "App is starting"} - + logCh <- logEntry{time.Now(), logInfo, "App is shutting down"} time.Sleep(100 * time.Millisecond) doneCh <- struct{}{} } - func logger() { for { select { - case entry := <-logCh : - fmt.Printf("%v - [%v]%v\n", entry.time.Format("2006-01-02T15:04:05"), entry.severity, entry.message) - case <-doneCh: - break + case entry := <-logCh: + fmt.Printf("%v - [%v]%v\n", entry.time.Format("2006-01-02T15:04:05"), entry.severity, entry.message) + case <-doneCh: + break // default: // fmt.Println("Default") } } -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/Functions/functions.go b/src/github.com/statho7/GoTutorial/Functions/functions.go index 24fb816..c4fc18e 100644 --- a/src/github.com/statho7/GoTutorial/Functions/functions.go +++ b/src/github.com/statho7/GoTutorial/Functions/functions.go @@ -121,7 +121,7 @@ import ( // return a / b, nil // } -func main() { +func main() { // func () { // msg := "Hello Go!" // fmt.Println(msg) @@ -132,7 +132,7 @@ func main() { // fmt.Println(i) // }(i) // } - + // var f func() = func() { // msg := "Hello Go!" // fmt.Println(msg) @@ -160,7 +160,7 @@ func main() { g := greeter{ greeting: "Hello", - name: "Go", + name: "Go", } g.greet() fmt.Println("The new name is: ", g.name) @@ -168,7 +168,7 @@ func main() { type greeter struct { greeting string - name string + name string } func (g greeter) greet() { @@ -179,4 +179,4 @@ func (g greeter) greet() { // func (g *greeter) greet() { // fmt.Println(g.greeting, g.name) // g.name = "" -// } \ No newline at end of file +// } diff --git a/src/github.com/statho7/GoTutorial/Goroutines/goroutines.go b/src/github.com/statho7/GoTutorial/Goroutines/goroutines.go index 1bb634b..aa8f01d 100644 --- a/src/github.com/statho7/GoTutorial/Goroutines/goroutines.go +++ b/src/github.com/statho7/GoTutorial/Goroutines/goroutines.go @@ -55,4 +55,4 @@ import ( func main() { runtime.GOMAXPROCS(100) fmt.Printf("Threads: %v", runtime.GOMAXPROCS(-1)) -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/Interfaces/interfaces.go b/src/github.com/statho7/GoTutorial/Interfaces/interfaces.go index 8adc1c6..969741e 100644 --- a/src/github.com/statho7/GoTutorial/Interfaces/interfaces.go +++ b/src/github.com/statho7/GoTutorial/Interfaces/interfaces.go @@ -145,4 +145,4 @@ func main() { default: fmt.Println("I don't know what type of i is") } -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/arrays_and_slices/arrays_and_slices.go b/src/github.com/statho7/GoTutorial/arrays_and_slices/arrays_and_slices.go index 829eda1..d4b6294 100644 --- a/src/github.com/statho7/GoTutorial/arrays_and_slices/arrays_and_slices.go +++ b/src/github.com/statho7/GoTutorial/arrays_and_slices/arrays_and_slices.go @@ -4,18 +4,18 @@ import ( "fmt" ) -func main() { +func main() { // grade1 := 97 // grade2 := 85 // grade3 := 93 // fmt.Printf("Grades: %v, %v, %v", grade1, grade2, grade3) - + // grades := [3]int{97,85,93} // fmt.Printf("Grades: %v", grades) - + // grades := [...]int{97,85,93} // fmt.Printf("Grades: %v", grades) - + // var students [3]string // fmt.Printf("Students: %v\n", students) // students[0] = "Lisa" @@ -24,7 +24,7 @@ func main() { // fmt.Printf("Students: %v\n", students) // fmt.Printf("Students #1: %v\n", students[1]) // fmt.Printf("Number of students: %v", len(students)) - + // var identityMatrix [3][3]int = [3][3]int { [3]int{1,0,0}, [3]int{0,1,0}, [3]int{0,0,1}} // fmt.Println(identityMatrix) @@ -103,19 +103,19 @@ func main() { // b := append(a[:2], a[3:]...) // fmt.Println(b) // fmt.Println(a)a[4] = 100 - + var a [5]int - fmt.Println("emp:", a) - fmt.Println("set:", a) - fmt.Println("get:", a[4]) + fmt.Println("emp:", a) + fmt.Println("set:", a) + fmt.Println("get:", a[4]) fmt.Println("len:", len(a)) b := [5]int{1, 2, 3, 4, 5} - fmt.Println("dcl:", b) + fmt.Println("dcl:", b) var twoD [2][3]int - for i := 0; i < 2; i++ { - for j := 0; j < 3; j++ { - twoD[i][j] = i + j - } - } - fmt.Println("2d: ", twoD) -} \ No newline at end of file + for i := 0; i < 2; i++ { + for j := 0; j < 3; j++ { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) +} diff --git a/src/github.com/statho7/GoTutorial/constants/constants.go b/src/github.com/statho7/GoTutorial/constants/constants.go index bc7feef..8ae98cd 100644 --- a/src/github.com/statho7/GoTutorial/constants/constants.go +++ b/src/github.com/statho7/GoTutorial/constants/constants.go @@ -120,9 +120,9 @@ const ( canSeeSouthAmerica ) -func main() { +func main() { var roles byte = isAdmin | canSeeFinancials | canSeeEurope fmt.Printf("%b\n", roles) - fmt.Printf("Is Admin ? %v\n", isAdmin & roles == isAdmin) - fmt.Printf("Is HQ ? %v", isHeadquarters & roles == isHeadquarters) -} \ No newline at end of file + fmt.Printf("Is Admin ? %v\n", isAdmin&roles == isAdmin) + fmt.Printf("Is HQ ? %v", isHeadquarters&roles == isHeadquarters) +} diff --git a/src/github.com/statho7/GoTutorial/defer_panic_recover/yolo.go b/src/github.com/statho7/GoTutorial/defer_panic_recover/yolo.go index 0175365..2fdef23 100644 --- a/src/github.com/statho7/GoTutorial/defer_panic_recover/yolo.go +++ b/src/github.com/statho7/GoTutorial/defer_panic_recover/yolo.go @@ -8,13 +8,13 @@ package main // fmt.Println("start") // fmt.Println("middle") // fmt.Println("end") - + // fmt.Println("~*~") // fmt.Println("start") // defer fmt.Println("middle") // fmt.Println("end") - + // fmt.Println("~*~") // defer fmt.Println("start") @@ -52,11 +52,11 @@ package main // // a := "start" // // defer fmt.Println(a) // // a = "end" - + // // a, b := 1, 0 // // ans := a / b // // fmt.Println(ans) - + // fmt.Println("start") // panic("Something bad happened") // fmt.Println("end") @@ -81,12 +81,12 @@ package main // "log" // ) -// func main() { +// func main() { // // fmt.Println("start") // // defer fmt.Println("this was deferred") // // panic("Something bad happened") // // fmt.Println("end") - + // fmt.Println("start") // defer func() { // if err:= recover(); err != nil { @@ -102,7 +102,7 @@ import ( "log" ) -func main() { +func main() { fmt.Println("start") panicker() fmt.Println("end") @@ -111,10 +111,10 @@ func main() { func panicker() { fmt.Println("about to panic!") defer func() { - if err:= recover(); err != nil { + if err := recover(); err != nil { log.Println("Error:", err) // panic(err) } }() panic("Something bad happened") -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/firstapp/hello-world.go b/src/github.com/statho7/GoTutorial/firstapp/hello-world.go index 8b045d3..ee75b1c 100644 --- a/src/github.com/statho7/GoTutorial/firstapp/hello-world.go +++ b/src/github.com/statho7/GoTutorial/firstapp/hello-world.go @@ -3,5 +3,5 @@ package main import "fmt" func main() { - fmt.Println("hello go!") -} \ No newline at end of file + fmt.Println("hello go!") +} diff --git a/src/github.com/statho7/GoTutorial/if_and_switch/if_and_switch.go b/src/github.com/statho7/GoTutorial/if_and_switch/if_and_switch.go index 828774c..8079177 100644 --- a/src/github.com/statho7/GoTutorial/if_and_switch/if_and_switch.go +++ b/src/github.com/statho7/GoTutorial/if_and_switch/if_and_switch.go @@ -9,7 +9,7 @@ func returnTrue() bool { return true } -func main() { +func main() { // if false { // fmt.Println("The test is true") // } else { @@ -38,7 +38,7 @@ func main() { // fmt.Println("The guess must be greater than 1") // } else if guess > 100 { // fmt.Println("The guess must be less than 100") - // } else { + // } else { // if guess < number { // fmt.Println("Too low!") // } @@ -48,7 +48,7 @@ func main() { // if guess == number { // fmt.Println("You got it!") // } - + // fmt.Println(number <= guess, number >= guess, number != guess) // } @@ -56,34 +56,34 @@ func main() { myNum := 0.123 // if myNum == math.Pow(math.Sqrt(myNum), 2){ - if math.Abs(myNum / math.Pow(math.Sqrt(myNum), 2) - 1 ) < 0.001{ + if math.Abs(myNum/math.Pow(math.Sqrt(myNum), 2)-1) < 0.001 { fmt.Println("These are the same") } else { fmt.Println("These are different") } - + switch returnTrue() { case true: fmt.Println("Yolo") - // fallthrough ---> bad, too bad be careful + // fallthrough ---> bad, too bad be careful case false: - fmt.Println("Swag") + fmt.Println("Swag") default: fmt.Println("Thug") } - - var i interface{}="1." + + var i interface{} = "1." switch i.(type) { case int: fmt.Println("i is int") - // fallthrough ---> bad, too bad be careful + // fallthrough ---> bad, too bad be careful case float64: - fmt.Println("i is float64") + fmt.Println("i is float64") case string: - fmt.Println("i is string") + fmt.Println("i is string") break fmt.Println("This won't print") default: fmt.Println("i is an helicopter") } -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/looping/looping.go b/src/github.com/statho7/GoTutorial/looping/looping.go index 0c9bd9e..3cd4fbe 100644 --- a/src/github.com/statho7/GoTutorial/looping/looping.go +++ b/src/github.com/statho7/GoTutorial/looping/looping.go @@ -4,12 +4,12 @@ import ( "fmt" ) -func main() { +func main() { // count := 10 // for i := 0; i < count; i++ { // fmt.Println(i) // } - + // for i, j := 0, 0; i < count; i, j = i + 1, j + 2 { // fmt.Println(i, j) // } @@ -43,7 +43,6 @@ func main() { // } // } - // i := 0 // for i < count { // if i % 9 == 0 { @@ -67,7 +66,7 @@ func main() { // } // if (i * j) > 30{ // break Loop - // } + // } // } // } @@ -97,4 +96,4 @@ func main() { for k, v := range s { fmt.Println(k, string(v)) } -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/maps_and_structs/maps_and_structs.go b/src/github.com/statho7/GoTutorial/maps_and_structs/maps_and_structs.go index f036e2a..3cf549c 100644 --- a/src/github.com/statho7/GoTutorial/maps_and_structs/maps_and_structs.go +++ b/src/github.com/statho7/GoTutorial/maps_and_structs/maps_and_structs.go @@ -143,7 +143,6 @@ package main // fmt.Println(sp.age) // } - // Composition // type Animal struct { // Name string @@ -176,20 +175,19 @@ package main // fmt.Println(b.CanFly) // } - -import( +import ( "fmt" "reflect" ) type Animal struct { - Name string `required max:"100"` + Name string `required max:"100"` Origin string } -func main() { +func main() { t := reflect.TypeOf(Animal{}) field, _ := t.FieldByName(("Name")) fmt.Println(field.Tag) -} \ No newline at end of file +} diff --git a/src/github.com/statho7/GoTutorial/pointers/pointers.go b/src/github.com/statho7/GoTutorial/pointers/pointers.go index 46b5051..0a0a3b9 100644 --- a/src/github.com/statho7/GoTutorial/pointers/pointers.go +++ b/src/github.com/statho7/GoTutorial/pointers/pointers.go @@ -4,7 +4,7 @@ import ( "fmt" ) -func main() { +func main() { // a := 42 // b := a // fmt.Println(a, b) @@ -16,7 +16,7 @@ func main() { // fmt.Println(a, &a, b, *b) // *b = 14 // fmt.Println(a, &a, b, *b) - + // a := [3]int{1, 2, 3} // b := &a[0] // c := &a[1] @@ -32,14 +32,13 @@ func main() { // ms.foo = 42 // fmt.Println(ms.foo) - // a := []int{1, 2, 3} // b := a // fmt.Println(a, b) // a[1] = 42 // fmt.Println(a, b) - - a := map[string]string {"foo": "bar", "baz": "buz"} + + a := map[string]string{"foo": "bar", "baz": "buz"} b := a fmt.Println(a, b) a["foo"] = "qux" @@ -48,4 +47,4 @@ func main() { // type myStruct struct { // foo int -// } \ No newline at end of file +// } diff --git a/src/github.com/statho7/GoTutorial/primitives/primitives.go b/src/github.com/statho7/GoTutorial/primitives/primitives.go index 515dcc6..0bb4754 100644 --- a/src/github.com/statho7/GoTutorial/primitives/primitives.go +++ b/src/github.com/statho7/GoTutorial/primitives/primitives.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "fmt" ) // func main() { @@ -40,7 +40,6 @@ import ( // // fmt.Println(a % b) // } - // // bit operations // func main() { // a := 10 @@ -87,13 +86,12 @@ import ( // // fmt.Println(a - b) // // fmt.Println(a * b) // // fmt.Println(a / b) - + // // var n complex64 = 1 + 2i // // var n complex128 = 1 + 2i // // fmt.Printf("%v, %T\n",real(n),real(n)) // // fmt.Printf("%v, %T\n",imag(n),imag(n)) - // var n complex128 = complex(5,12) // fmt.Printf("%v, %T\n",n,n) // } @@ -118,5 +116,5 @@ import ( func main() { // r:= 'a' var r rune = 'a' - fmt.Printf("%v, %T\n",r,r) -} \ No newline at end of file + fmt.Printf("%v, %T\n", r, r) +} diff --git a/src/github.com/statho7/GoTutorial/variables/variables.go b/src/github.com/statho7/GoTutorial/variables/variables.go index 869f266..e70b64f 100644 --- a/src/github.com/statho7/GoTutorial/variables/variables.go +++ b/src/github.com/statho7/GoTutorial/variables/variables.go @@ -25,7 +25,7 @@ package main // ) // func main() { - + // } // var j int = 27 @@ -41,22 +41,21 @@ package main // func main() { // var i int = 42 // fmt.Printf("%v, %T\n", i, i) - + // var j float32 // j = float32(i) // fmt.Printf("%v, %T\n", j, j) // } - import ( - "fmt" - "strconv" + "fmt" + "strconv" ) func main() { - var i int = 42 - fmt.Printf("%v, %T\n", i, i) - - var j string = strconv.Itoa(i) - fmt.Printf("%v, %T\n", j, j) -} \ No newline at end of file + var i int = 42 + fmt.Printf("%v, %T\n", i, i) + + var j string = strconv.Itoa(i) + fmt.Printf("%v, %T\n", j, j) +} diff --git a/src/github.com/statho7/LearningGo/README.md b/src/github.com/statho7/LearningGo/README.md new file mode 100644 index 0000000..1d74b72 --- /dev/null +++ b/src/github.com/statho7/LearningGo/README.md @@ -0,0 +1,2 @@ +## Learning Go by Jon Bodner +https://learning.oreilly.com/library/view/learning-go/9781492077206 \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/arrays/arrays.go b/src/github.com/statho7/LearningGo/arrays/arrays.go new file mode 100644 index 0000000..a2a383f --- /dev/null +++ b/src/github.com/statho7/LearningGo/arrays/arrays.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func main(){ + var x1 = [...]int{10, 20, 30} + fmt.Println(x1) + + var x2 = [...]int{1, 2, 3} + var y = [3]int{1, 2, 3} + fmt.Println(x2 == y) + + var x3 [2][3]int + fmt.Println(x3) +} \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/ch1/Makefile b/src/github.com/statho7/LearningGo/ch1/Makefile new file mode 100644 index 0000000..fbde3d3 --- /dev/null +++ b/src/github.com/statho7/LearningGo/ch1/Makefile @@ -0,0 +1,17 @@ +.DEFAULT_GOAL := build + +fmt: + go fmt ./... +.PHONY:fmt + +lint: fmt + golint ./... +.PHONY:lint + +vet: fmt + go vet ./... +.PHONY:vet + +build: vet + go build hello.go +.PHONY:build \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/ch1/hello.exe b/src/github.com/statho7/LearningGo/ch1/hello.exe new file mode 100644 index 0000000..6de3ec8 Binary files /dev/null and b/src/github.com/statho7/LearningGo/ch1/hello.exe differ diff --git a/src/github.com/statho7/LearningGo/ch1/hello.go b/src/github.com/statho7/LearningGo/ch1/hello.go new file mode 100644 index 0000000..f7b60bd --- /dev/null +++ b/src/github.com/statho7/LearningGo/ch1/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, world!") +} diff --git a/src/github.com/statho7/LearningGo/ch1/hello_world.exe b/src/github.com/statho7/LearningGo/ch1/hello_world.exe new file mode 100644 index 0000000..6de3ec8 Binary files /dev/null and b/src/github.com/statho7/LearningGo/ch1/hello_world.exe differ diff --git a/src/github.com/statho7/LearningGo/complex_numbers/complex_numbers.go b/src/github.com/statho7/LearningGo/complex_numbers/complex_numbers.go new file mode 100644 index 0000000..490d72d --- /dev/null +++ b/src/github.com/statho7/LearningGo/complex_numbers/complex_numbers.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" +"math/cmplx" +) + +func main() { + x := complex(2.5, 3.1) + y := complex(10.2, 2) + fmt.Println(x + y) + fmt.Println(x - y) + fmt.Println(x * y) + fmt.Println(x / y) + fmt.Println(real(x)) + fmt.Println(imag(x)) + fmt.Println(cmplx.Abs(x)) +} \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/const/const.go b/src/github.com/statho7/LearningGo/const/const.go new file mode 100644 index 0000000..5aaf2d7 --- /dev/null +++ b/src/github.com/statho7/LearningGo/const/const.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" +) + +const x int64 = 10 + +const ( + idKey = "id" + nameKey = "name" +) + +const z = 20 * 10 + +func main() { + const y = "hello" + + fmt.Println(x) + fmt.Println(y) + + x = x + 1 + y = "bye" + + fmt.Println(x) + fmt.Println(y) +} \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/slices/capacity/capacity.go b/src/github.com/statho7/LearningGo/slices/capacity/capacity.go new file mode 100644 index 0000000..8d1eb3c --- /dev/null +++ b/src/github.com/statho7/LearningGo/slices/capacity/capacity.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func main() { + var x []int + fmt.Println(x, len(x), cap(x)) + x = append(x, 10) + fmt.Println(x, len(x), cap(x)) + x = append(x, 20) + fmt.Println(x, len(x), cap(x)) + x = append(x, 30) + fmt.Println(x, len(x), cap(x)) + x = append(x, 40) + fmt.Println(x, len(x), cap(x)) + x = append(x, 50) + fmt.Println(x, len(x), cap(x)) +} \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/slices/slice_of_slice/slice_of_slice.go b/src/github.com/statho7/LearningGo/slices/slice_of_slice/slice_of_slice.go new file mode 100644 index 0000000..852f197 --- /dev/null +++ b/src/github.com/statho7/LearningGo/slices/slice_of_slice/slice_of_slice.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + x := []int{1, 2, 3, 4} + y := x[:2] + z := x[1:] + d := x[1:3] + e := x[:] + fmt.Println("x:", x) + fmt.Println("y:", y) + fmt.Println("z:", z) + fmt.Println("d:", d) + fmt.Println("e:", e) +} \ No newline at end of file diff --git a/src/github.com/statho7/LearningGo/slices/sparse_slice/sparse_slice.go b/src/github.com/statho7/LearningGo/slices/sparse_slice/sparse_slice.go new file mode 100644 index 0000000..6f5dde2 --- /dev/null +++ b/src/github.com/statho7/LearningGo/slices/sparse_slice/sparse_slice.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + var x = []int{1, 5: 4, 6, 10: 100, 15} + fmt.Println(x) +} diff --git a/src/github.com/statho7/LearningGo/sparse_array/sparse_array.go b/src/github.com/statho7/LearningGo/sparse_array/sparse_array.go new file mode 100644 index 0000000..54c6a8f --- /dev/null +++ b/src/github.com/statho7/LearningGo/sparse_array/sparse_array.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main(){ + // If you have a sparse array (an array where most elements are set to their zero value), you can specify only the indices with values in the array literal: + var x = [12]int{1, 5: 4, 6, 10: 100, 15} + fmt.Println(x) +} \ No newline at end of file diff --git a/src/github.com/statho7/TDD/hello.go b/src/github.com/statho7/TDD/hello.go index d55525d..a53bfbb 100644 --- a/src/github.com/statho7/TDD/hello.go +++ b/src/github.com/statho7/TDD/hello.go @@ -3,9 +3,9 @@ package main import "fmt" func Hello() string { - return "Hello, world" + return "Hello, world" } func main() { - fmt.Println(Hello()) -} \ No newline at end of file + fmt.Println(Hello()) +} diff --git a/src/github.com/statho7/TDD/hello_test.go b/src/github.com/statho7/TDD/hello_test.go index 7a52ad6..d424f74 100644 --- a/src/github.com/statho7/TDD/hello_test.go +++ b/src/github.com/statho7/TDD/hello_test.go @@ -3,12 +3,12 @@ package main import "testing" func TestHello(t *testing.T) { - // got := Hello() - got := Hello("Chris") - // want := "Hello, world" - want := "Hello, Chris" + // got := Hello() + got := Hello("Chris") + // want := "Hello, world" + want := "Hello, Chris" - if got != want { - t.Errorf("got %q want %q", got, want) - } -} \ No newline at end of file + if got != want { + t.Errorf("got %q want %q", got, want) + } +} diff --git a/src/github.com/statho7/TheGoProgrammingLanguage/README.md b/src/github.com/statho7/TheGoProgrammingLanguage/README.md new file mode 100644 index 0000000..1f744e9 --- /dev/null +++ b/src/github.com/statho7/TheGoProgrammingLanguage/README.md @@ -0,0 +1 @@ +https://learning.oreilly.com/library/view/the-go-programming/9780134190570/ \ No newline at end of file diff --git a/src/github.com/statho7/UltimateGoProgramming/README.md b/src/github.com/statho7/UltimateGoProgramming/README.md new file mode 100644 index 0000000..ded3e9f --- /dev/null +++ b/src/github.com/statho7/UltimateGoProgramming/README.md @@ -0,0 +1 @@ +Code that I wrote for Ultimate Go Programming , Second Edition by William Kennedy \ No newline at end of file