Configure environment variable
export DATABASE_URL=postgres://postgres@localhost/dbname?sslmode=disable
Run in CLI
go run main.go -page 1
Configure environment variable
export DATABASE_URL=postgres://postgres@localhost/dbname?sslmode=disable
Run in CLI
go run main.go -page 1
func newFileName() string { | |
buff := make([]byte, 6) | |
rand.Read(buff) | |
return fmt.Sprintf("%v-%X\n", time.Now().UTC().Format("2006-01-02T150405"), buff) | |
} |
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"syscall" |
package helper | |
// closer close descriptor and log error | |
func closer(f io.Closer) { | |
err := f.Close() | |
if err != nil { | |
log.Errorln("closing ", err) | |
} | |
} |
func execHelper(path, name string, arg ...string) (err error) { | |
cmd := exec.Command(name, arg...) | |
cmd.Dir = path | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
err = cmd.Run() | |
return | |
} |
// https://play.golang.org/p/8SjYn-cisj | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
"os" | |
) |
func closer(body io.Closer) { | |
err := body.Close() | |
if err != nil { | |
log.Errorln(err) | |
} | |
} | |
// defer closer(fileDescriptor) |
func TestHTTPHandler(t *testing.T) { | |
w := httptest.NewRecorder() | |
expected := "{\n\t\"error\": \"test error\"\n}\n" | |
HTTPHandler(w, nil) | |
b := w.Body.Bytes() | |
if string(b) != expected { | |
t.Errorf("expected %q, want %q", expected, string(b)) |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
) |
package fibonacci | |
type Memoized func(int) int | |
var fibMem = Memoize(fib) | |
func Memoize(mf Memoized) Memoized { | |
cache := make(map[int]int) | |
return func(key int) int { | |
if val, found := cache[key]; found { | |
return val |