diff --git a/050-env/050-env.go b/050-env/050-env.go deleted file mode 100644 index 593bfec..0000000 --- a/050-env/050-env.go +++ /dev/null @@ -1,27 +0,0 @@ -package main // Use the `os` package to list, set, and get - // environment variables. -import ( - "os" - "fmt" - "strings" -) - -func main() { - for _, e := range os.Environ() { // `Environ` returns a slice of strings in the form - pair := strings.Split(e, "=") // `KEY=value`. You can `strings.Split` them to get - fmt.Println(pair[0]) // the key and value. - } - fmt.Println() - - os.Setenv("FOO", "bar") // `Setenv` sets a given key, value pair. - fmt.Println(os.Getenv("FOO")) // `Getenv` returns the value at key, or an empty -} // string if the key isn't present. - -/* -$ go run env.go -HOME -PATH -PWD -... -bar -*/ diff --git a/050-env/env.go b/050-env/env.go new file mode 100644 index 0000000..45a54e0 --- /dev/null +++ b/050-env/env.go @@ -0,0 +1,26 @@ +// ## Env + +package main + +// Use the `os` package to list, set, and get +// environment variables. +import "os" +import "strings" +import "fmt" + +func main() { + // `os.Environ` returns a slice of strings in the form + // `KEY=value`. You can `strings.Split` them to get + // the key and value. + for _, e := range os.Environ() { + pair := strings.Split(e, "=") + fmt.Println(pair[0]) + } + fmt.Println() + + // `Setenv` sets a given key, value pair. + // `Getenv` returns the value at key, or an empty + // string if the key isn't present. + os.Setenv("FOO", "bar") + fmt.Println(os.Getenv("FOO")) +} diff --git a/050-env/env.sh b/050-env/env.sh new file mode 100644 index 0000000..968d927 --- /dev/null +++ b/050-env/env.sh @@ -0,0 +1,6 @@ +$ go run env.go +HOME +PATH +PWD +... +bar diff --git a/051-epoch/051-epoch.go b/051-epoch/051-epoch.go deleted file mode 100644 index 44f1ea0..0000000 --- a/051-epoch/051-epoch.go +++ /dev/null @@ -1,20 +0,0 @@ -package main // Use `time.Now` with `Unix` or `UnixNane` to get - // elapsed time since the Unix epoch. -import "time" - -func main() { - now := time.Now() - secs := now.Unix() - nanos := now.UnixNano() - millis := nanos / 1000000 // There is no `UnixMillis`. - println("Secs: ", secs) - println("Millis:", millis) - println("Nanos: ", nanos) -} - -/* -$ go run 051-epoch.go -Secs: 1348240948 -Millis: 1348240948517 -Nanos: 1348240948517870000 -*/ diff --git a/051-epoch/epoch.go b/051-epoch/epoch.go new file mode 100644 index 0000000..6eb3cc2 --- /dev/null +++ b/051-epoch/epoch.go @@ -0,0 +1,19 @@ +// ## Epoch + +package main + +import "time" + +func main() { + // Use `time.Now` with `Unix` or `UnixNane` to get + // elapsed time since the Unix epoch. + now := time.Now() + secs := now.Unix() + nanos := now.UnixNano() + + // There is no `UnixMillis`. + millis := nanos / 1000000 + println("Secs: ", secs) + println("Millis:", millis) + println("Nanos: ", nanos) +} diff --git a/051-epoch/epoch.sh b/051-epoch/epoch.sh new file mode 100644 index 0000000..2c1048c --- /dev/null +++ b/051-epoch/epoch.sh @@ -0,0 +1,4 @@ +$ go run 051-epoch.go +Secs: 1348240948 +Millis: 1348240948517 +Nanos: 1348240948517870000 diff --git a/052-errors/052-errors.go b/052-errors/errors.go similarity index 78% rename from 052-errors/052-errors.go rename to 052-errors/errors.go index 00f37c1..b78c3cd 100644 --- a/052-errors/052-errors.go +++ b/052-errors/errors.go @@ -1,3 +1,5 @@ +// ## Errors + package main import ("fmt"; "errors") @@ -18,6 +20,5 @@ func main() { fmt.Println(e) } -// == todo -// custom errors -// data conveying errors +// todo: custom errors +// todo: data conveying errors diff --git a/053-exec/053-exec.go b/053-exec/exec.go similarity index 79% rename from 053-exec/053-exec.go rename to 053-exec/exec.go index 486f141..3cf232e 100644 --- a/053-exec/053-exec.go +++ b/053-exec/exec.go @@ -1,6 +1,10 @@ +// ## Exec + package main -import ("syscall"; "os"; "os/exec") +import "syscall" +import "os" +import "os/exec" func main() { binary, lookErr := exec.LookPath("ls") diff --git a/054-exit/054-exit.go b/054-exit/054-exit.go deleted file mode 100644 index ddfad46..0000000 --- a/054-exit/054-exit.go +++ /dev/null @@ -1,21 +0,0 @@ -package main // Use `os.Exit` to immediatly exit with a given - // status. -import "os" - -func main() { - defer println("!") // This `println` will never be reached. - os.Exit(3) -} - -/* -$ go run exit.go // If you run `exit.go` using `go run`, the exit -exit status 3 // will be picked up by `go` and printed. - -$ go build exit.go // By building and executing a binary you can see -$ ./exit // the status in the terminal -$ echo $? -3 -*/ - -// == todo -// discuss building before getting here diff --git a/054-exit/exit.go b/054-exit/exit.go new file mode 100644 index 0000000..d8dec26 --- /dev/null +++ b/054-exit/exit.go @@ -0,0 +1,16 @@ +// Exit + +package main + +// Use `os.Exit` to immediatly exit with a given +// status. +import "os" + +func main() { + // This `println` will never be reached because the + // exit is immediate. + defer println("!") + os.Exit(3) +} + +// todo: discuss building before getting here diff --git a/054-exit/exit.sh b/054-exit/exit.sh new file mode 100644 index 0000000..c3539ba --- /dev/null +++ b/054-exit/exit.sh @@ -0,0 +1,11 @@ +# If you run `exit.go` using `go run`, the exit +# will be picked up by `go` and printed. +$ go run exit.go +exit status 3 + +# By building and executing a binary you can see +# the status in the terminal. +$ go build exit.go +$ ./exit +$ echo $? +3 diff --git a/055-file-open/055-file-open.go b/055-file-open/file-open.go similarity index 86% rename from 055-file-open/055-file-open.go rename to 055-file-open/file-open.go index 7dc5739..cd25a7a 100644 --- a/055-file-open/055-file-open.go +++ b/055-file-open/file-open.go @@ -1,6 +1,9 @@ +// ## File Open + package main -import ("fmt"; "os") +import "os" +import "fmt" func main() { file, err := os.Open("xx-file-open.go") diff --git a/056-file-read/056-file-read.go b/056-file-read/file-read.go similarity index 76% rename from 056-file-read/056-file-read.go rename to 056-file-read/file-read.go index ef5369d..c636c8a 100644 --- a/056-file-read/056-file-read.go +++ b/056-file-read/file-read.go @@ -1,6 +1,9 @@ +// ## File Read + package main -import ("fmt"; "io/ioutil") +import "io/ioutil" +import "fmt" func main() { contents, err := ioutil.ReadFile("xx-file-read.go") diff --git a/057-file-write/057-file-write.go b/057-file-write/file-write.go similarity index 91% rename from 057-file-write/057-file-write.go rename to 057-file-write/file-write.go index 8dd64df..f25b83d 100644 --- a/057-file-write/057-file-write.go +++ b/057-file-write/file-write.go @@ -1,3 +1,5 @@ +// ## File Write + package main import "os" diff --git a/058-flags/058-flags.go b/058-command-line-flags/command-line-flags.go similarity index 55% rename from 058-flags/058-flags.go rename to 058-command-line-flags/command-line-flags.go index e154258..64a8092 100644 --- a/058-flags/058-flags.go +++ b/058-command-line-flags/command-line-flags.go @@ -1,6 +1,9 @@ +// ## Command Line Flags + package main -import ("fmt"; "flag") +import "flag" +import "fmt" func main() { maxp := flag.Int("repeat", 3, "time to repeat args") @@ -12,8 +15,7 @@ func main() { } } -// todoo -// multiple flags -// trailing args -// arg escaping -// help text and usage errors +// todo: multiple flags +// todo: trailing args +// todo: arg escaping +// todo: help text and usage errors diff --git a/059-http-client-basic/059-http-client-basic.go b/059-http-client-basic/http-client-basic.go similarity index 81% rename from 059-http-client-basic/059-http-client-basic.go rename to 059-http-client-basic/http-client-basic.go index b48a850..06531cc 100644 --- a/059-http-client-basic/059-http-client-basic.go +++ b/059-http-client-basic/http-client-basic.go @@ -1,6 +1,10 @@ +// ## HTTP Client Basic Auth + package main -import ("net/http"; "io/ioutil"; "fmt") +import "net/http" +import "io/ioutil" +import "fmt" func main() { req, _ := http.NewRequest("GET", "http://127.0.0.1:5000/", nil) diff --git a/060-http-client/060-http-client.go b/060-http-client/http-client.go similarity index 74% rename from 060-http-client/060-http-client.go rename to 060-http-client/http-client.go index a2c6b39..79a043b 100644 --- a/060-http-client/060-http-client.go +++ b/060-http-client/http-client.go @@ -1,6 +1,10 @@ +// ## HTTP Client + package main -import ("net/http"; "io/ioutil"; "fmt") +import "net/http" +import "io/ioutil" +import "fmt" func main() { resp, err := http.Get("http://127.0.0.1:5000/") diff --git a/061-http-server-basic/061-http-server-basic.go b/061-http-server-basic/http-server-basic.go similarity index 97% rename from 061-http-server-basic/061-http-server-basic.go rename to 061-http-server-basic/http-server-basic.go index 531d110..8b6d811 100644 --- a/061-http-server-basic/061-http-server-basic.go +++ b/061-http-server-basic/http-server-basic.go @@ -1,10 +1,12 @@ +// ## HTTP Server Basic + package main import ( - "fmt" "net/http" "encoding/base64" "strings" + "fmt" ) type Authenticator func(string, string) bool diff --git a/tool/index.txt b/tool/index.txt index aa79407..c8b3140 100644 --- a/tool/index.txt +++ b/tool/index.txt @@ -55,7 +55,7 @@ exit file-open file-read file-write -flags +command-line-flags http-client-basic http-client http-server-basic