Skip to content

Commit 2beedb9

Browse files
committed
Initial commit.
0 parents  commit 2beedb9

28 files changed

+629
-0
lines changed

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2015, Matt Butcher and Matt Farina
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Go in Practice
2+
3+
The source code repository for Manning Publications Go in Practice.

chapter1/channel.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func printCount(c chan int) {
9+
num := 0
10+
for num >= 0 {
11+
num = <-c
12+
fmt.Print(num, " ")
13+
}
14+
}
15+
16+
func main() {
17+
c := make(chan int)
18+
a := []int{8, 6, 7, 5, 3, 0, 9, -1}
19+
20+
go printCount(c)
21+
22+
for _, v := range a {
23+
c <- v
24+
}
25+
time.Sleep(time.Millisecond * 1)
26+
fmt.Println("End of main")
27+
}

chapter1/coverage.out

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mode: set
2+
go-in-practice/ch1/hello.go:5.23,7.2 1 1
3+
go-in-practice/ch1/hello.go:9.13,12.2 2 0

chapter1/goroutine.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func count() {
9+
for i := 0; i < 5; i++ {
10+
fmt.Println(i)
11+
time.Sleep(time.Millisecond * 1)
12+
}
13+
}
14+
15+
func main() {
16+
go count()
17+
time.Sleep(time.Millisecond * 2)
18+
fmt.Println("Hello World")
19+
time.Sleep(time.Millisecond * 5)
20+
}

chapter1/hello.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func getName() string {
6+
return "World!"
7+
}
8+
9+
func main() {
10+
name := getName()
11+
fmt.Println("Hello ", name)
12+
}

chapter1/hello_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestName(t *testing.T) {
6+
name := getName()
7+
8+
if name != "World!" {
9+
t.Error("Respone from getName is unexpected value")
10+
}
11+
}

chapter1/http_get.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
resp, _ := http.Get("http://example.com/")
11+
body, _ := ioutil.ReadAll(resp.Body)
12+
fmt.Println(string(body))
13+
resp.Body.Close()
14+
}

chapter1/inigo.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func hello(res http.ResponseWriter, req *http.Request) {
9+
fmt.Fprint(res, "Hello, my name is Inigo Montoya")
10+
}
11+
12+
func main() {
13+
http.HandleFunc("/", hello)
14+
http.ListenAndServe("localhost:4000", nil)
15+
}

chapter1/read_status.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"net"
7+
)
8+
9+
func main() {
10+
conn, _ := net.Dial("tcp", "golang.org:80")
11+
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
12+
status, _ := bufio.NewReader(conn).ReadString('\n')
13+
fmt.Println(status)
14+
}

chapter1/returns.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Names() (string, string) {
8+
return "Foo", "Bar"
9+
}
10+
11+
func main() {
12+
n1, n2 := Names()
13+
fmt.Println(n1, n2)
14+
15+
n3, _ := Names()
16+
fmt.Println(n3)
17+
}

chapter1/returns2.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Names() (first string, second string) {
8+
first = "Foo"
9+
second = "Bar"
10+
return
11+
}
12+
13+
func main() {
14+
n1, n2 := Names()
15+
fmt.Println(n1, n2)
16+
}

chapter2/callback_shutdown.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
)
8+
9+
func main() {
10+
http.HandleFunc("/shutdown", shutdown)
11+
http.HandleFunc("/", homePage)
12+
http.ListenAndServe(":8080", nil)
13+
}
14+
15+
func shutdown(res http.ResponseWriter, req *http.Request) {
16+
os.Exit(0)
17+
}
18+
19+
func homePage(res http.ResponseWriter, req *http.Request) {
20+
if req.URL.Path != "/" {
21+
http.NotFound(res, req)
22+
return
23+
}
24+
fmt.Fprint(res, "The homepage.")
25+
}

chapter2/conf.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; A comment line
2+
[Section]
3+
enabled = true
4+
path = /usr/local # another comment

chapter2/conf.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"enabled": true,
3+
"path": "/usr/local"
4+
}

chapter2/conf.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
enabled: true
2+
path: /usr/local

chapter2/count_cli.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/codegangsta/cli"
6+
"os"
7+
)
8+
9+
func main() {
10+
app := cli.NewApp()
11+
app.Usage = "Count up or down."
12+
app.Commands = []cli.Command{
13+
{
14+
Name: "up",
15+
ShortName: "u",
16+
Usage: "Count Up",
17+
Flags: []cli.Flag{
18+
cli.IntFlag{
19+
Name: "stop, s",
20+
Usage: "Value to count up to",
21+
Value: 10,
22+
},
23+
},
24+
Action: func(c *cli.Context) {
25+
start := c.Int("stop")
26+
if start <= 0 {
27+
fmt.Println("Stop cannot be negative.")
28+
}
29+
for i := 1; i <= start; i++ {
30+
fmt.Println(i)
31+
}
32+
},
33+
},
34+
{
35+
Name: "down",
36+
ShortName: "d",
37+
Usage: "Count Down",
38+
Flags: []cli.Flag{
39+
cli.IntFlag{
40+
Name: "start, s",
41+
Usage: "Start counting down from",
42+
Value: 10,
43+
},
44+
},
45+
Action: func(c *cli.Context) {
46+
start := c.Int("start")
47+
if start < 0 {
48+
fmt.Println("Start cannot be negative.")
49+
}
50+
for i := start; i >= 0; i-- {
51+
fmt.Println(i)
52+
}
53+
},
54+
},
55+
}
56+
57+
app.Run(os.Args)
58+
}

chapter2/env_config.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
)
8+
9+
func main() {
10+
http.HandleFunc("/", homePage)
11+
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
12+
}
13+
14+
func homePage(res http.ResponseWriter, req *http.Request) {
15+
if req.URL.Path != "/" {
16+
http.NotFound(res, req)
17+
return
18+
}
19+
fmt.Fprint(res, "The homepage.")
20+
}

chapter2/flag_cli.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
var name = flag.String("name", "World", "A name to say hello to.")
9+
10+
var spanish bool
11+
12+
func init() {
13+
flag.BoolVar(&spanish, "spanish", false, "Use Spanish language.")
14+
flag.BoolVar(&spanish, "s", false, "Use Spanish language.")
15+
}
16+
17+
func main() {
18+
flag.Parse()
19+
if spanish == true {
20+
fmt.Printf("Hola %s!\n", *name)
21+
} else {
22+
fmt.Printf("Hello %s!\n", *name)
23+
}
24+
}

chapter2/go_flags_example.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
flags "github.com/jessevdk/go-flags"
6+
)
7+
8+
var opts struct {
9+
Name string `short:"n" long:"name" default:"World" description:"A name to say hello to."`
10+
Spanish bool `short:"s" long:"spanish" description:"Use Spanish Language"`
11+
}
12+
13+
func main() {
14+
flags.Parse(&opts)
15+
16+
if opts.Spanish == true {
17+
fmt.Printf("Hola %s!\n", opts.Name)
18+
} else {
19+
fmt.Printf("Hello %s!\n", opts.Name)
20+
}
21+
}

chapter2/hello_cli.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/codegangsta/cli"
6+
"os"
7+
)
8+
9+
func main() {
10+
app := cli.NewApp()
11+
app.Name = "hello_cli"
12+
app.Usage = "Print hello world"
13+
app.Flags = []cli.Flag{
14+
cli.StringFlag{
15+
Name: "name, n",
16+
Value: "World",
17+
Usage: "Who to say hello to.",
18+
},
19+
}
20+
app.Action = func(c *cli.Context) {
21+
name := c.GlobalString("name")
22+
fmt.Printf("Hello %s!\n", name)
23+
}
24+
25+
app.Run(os.Args)
26+
}

0 commit comments

Comments
 (0)