updates
This commit is contained in:
parent
36c7bf4bef
commit
70dd607774
@ -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
26
050-env/env.go
Normal 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
6
050-env/env.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
$ go run env.go
|
||||||
|
HOME
|
||||||
|
PATH
|
||||||
|
PWD
|
||||||
|
...
|
||||||
|
bar
|
@ -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
19
051-epoch/epoch.go
Normal 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
4
051-epoch/epoch.sh
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
$ go run 051-epoch.go
|
||||||
|
Secs: 1348240948
|
||||||
|
Millis: 1348240948517
|
||||||
|
Nanos: 1348240948517870000
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Errors
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("fmt"; "errors")
|
import ("fmt"; "errors")
|
||||||
@ -18,6 +20,5 @@ func main() {
|
|||||||
fmt.Println(e)
|
fmt.Println(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// == todo
|
// todo: custom errors
|
||||||
// custom errors
|
// todo: data conveying errors
|
||||||
// data conveying errors
|
|
@ -1,6 +1,10 @@
|
|||||||
|
// ## Exec
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("syscall"; "os"; "os/exec")
|
import "syscall"
|
||||||
|
import "os"
|
||||||
|
import "os/exec"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
binary, lookErr := exec.LookPath("ls")
|
binary, lookErr := exec.LookPath("ls")
|
@ -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
16
054-exit/exit.go
Normal 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
11
054-exit/exit.sh
Normal 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
|
@ -1,6 +1,9 @@
|
|||||||
|
// ## File Open
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("fmt"; "os")
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file, err := os.Open("xx-file-open.go")
|
file, err := os.Open("xx-file-open.go")
|
@ -1,6 +1,9 @@
|
|||||||
|
// ## File Read
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("fmt"; "io/ioutil")
|
import "io/ioutil"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
contents, err := ioutil.ReadFile("xx-file-read.go")
|
contents, err := ioutil.ReadFile("xx-file-read.go")
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## File Write
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "os"
|
import "os"
|
@ -1,6 +1,9 @@
|
|||||||
|
// ## Command Line Flags
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("fmt"; "flag")
|
import "flag"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
maxp := flag.Int("repeat", 3, "time to repeat args")
|
maxp := flag.Int("repeat", 3, "time to repeat args")
|
||||||
@ -12,8 +15,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todoo
|
// todo: multiple flags
|
||||||
// multiple flags
|
// todo: trailing args
|
||||||
// trailing args
|
// todo: arg escaping
|
||||||
// arg escaping
|
// todo: help text and usage errors
|
||||||
// help text and usage errors
|
|
@ -1,6 +1,10 @@
|
|||||||
|
// ## HTTP Client Basic Auth
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("net/http"; "io/ioutil"; "fmt")
|
import "net/http"
|
||||||
|
import "io/ioutil"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
req, _ := http.NewRequest("GET", "http://127.0.0.1:5000/", nil)
|
req, _ := http.NewRequest("GET", "http://127.0.0.1:5000/", nil)
|
@ -1,6 +1,10 @@
|
|||||||
|
// ## HTTP Client
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("net/http"; "io/ioutil"; "fmt")
|
import "net/http"
|
||||||
|
import "io/ioutil"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
resp, err := http.Get("http://127.0.0.1:5000/")
|
resp, err := http.Get("http://127.0.0.1:5000/")
|
@ -1,10 +1,12 @@
|
|||||||
|
// ## HTTP Server Basic
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"strings"
|
"strings"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Authenticator func(string, string) bool
|
type Authenticator func(string, string) bool
|
@ -55,7 +55,7 @@ exit
|
|||||||
file-open
|
file-open
|
||||||
file-read
|
file-read
|
||||||
file-write
|
file-write
|
||||||
flags
|
command-line-flags
|
||||||
http-client-basic
|
http-client-basic
|
||||||
http-client
|
http-client
|
||||||
http-server-basic
|
http-server-basic
|
||||||
|
Loading…
x
Reference in New Issue
Block a user