This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func createDirIfNotExist(pathName string) (err error) { | |
| if _, err = os.Stat(pathName); err != nil { | |
| if os.IsNotExist(err) { | |
| err = os.MkdirAll(pathName, os.ModePerm) | |
| return | |
| } | |
| } | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import "crypto/rand" | |
| func RandStr(strSize int) (ret string, err error) { | |
| alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
| var bytes = make([]byte, strSize) | |
| _, err = rand.Read(bytes) | |
| if err != nil { | |
| return | |
| } | |
| for k, v := range bytes { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "os" | |
| ) | |
| func main() { | |
| input, err := ioutil.ReadAll(os.Stdin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/tls" | |
| "crypto/x509" | |
| "fmt" | |
| "io" | |
| "log" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func parseArray(value interface{}) string { | |
| switch value.(type) { | |
| case []interface{}: | |
| var aux string | |
| for _, v := range value.([]interface{}) { | |
| if aux != "" { | |
| aux += "," | |
| } | |
| aux += parseArray(v) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func FindSubmatch(text string, regex string, submatch int) (ret string) { | |
| var r = regexp.MustCompile(regex) | |
| a := r.FindStringSubmatch(text) | |
| if len(a) <= submatch { | |
| ret = "" | |
| return | |
| } | |
| ret = a[submatch] | |
| ret = strings.TrimSpace(ret) | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;;; Creates a fake NAND gate and then creates all gates from NAND. | |
| ;;;; This is just an exercise, obviously has no practical application. | |
| ;;; NAND | |
| (defun !& (a b) (if (and (= a 1) (= b 1)) 0 1)) | |
| ;;; NOT | |
| (defun ! (a) (!& a 1)) | |
| ;;; AND |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "fmt" | |
| "io" | |
| "os" | |
| "strings" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
OlderNewer