This commit is contained in:
Mark McGranaghan 2012-09-23 12:36:08 -07:00
parent 36c7bf4bef
commit 70dd607774
19 changed files with 123 additions and 84 deletions

View File

@ -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
*/

26
050-env/env.go Normal file
View File

@ -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"))
}

6
050-env/env.sh Normal file
View File

@ -0,0 +1,6 @@
$ go run env.go
HOME
PATH
PWD
...
bar

View File

@ -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
*/

19
051-epoch/epoch.go Normal file
View File

@ -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)
}

4
051-epoch/epoch.sh Normal file
View File

@ -0,0 +1,4 @@
$ go run 051-epoch.go
Secs: 1348240948
Millis: 1348240948517
Nanos: 1348240948517870000

View File

@ -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

View File

@ -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")

View File

@ -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

16
054-exit/exit.go Normal file
View File

@ -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

11
054-exit/exit.sh Normal file
View File

@ -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

View File

@ -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")

View File

@ -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")

View File

@ -1,3 +1,5 @@
// ## File Write
package main
import "os"

View File

@ -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

View File

@ -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)

View File

@ -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/")

View File

@ -1,10 +1,12 @@
// ## HTTP Server Basic
package main
import (
"fmt"
"net/http"
"encoding/base64"
"strings"
"fmt"
)
type Authenticator func(string, string) bool

View File

@ -55,7 +55,7 @@ exit
file-open
file-read
file-write
flags
command-line-flags
http-client-basic
http-client
http-server-basic