ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Go​Snippets

by ccirello ST2/ST3 Missing

Snippets of Go code for Sublime Text

Labels go, snippets

Details

Installs

  • Total 4K
  • Win 2K
  • OS X 792
  • Linux 608
Oct 18 Oct 17 Oct 16 Oct 15 Oct 14 Oct 13 Oct 12 Oct 11 Oct 10 Oct 9 Oct 8 Oct 7 Oct 6 Oct 5 Oct 4 Oct 3 Oct 2 Oct 1 Sep 30 Sep 29 Sep 28 Sep 27 Sep 26 Sep 25 Sep 24 Sep 23 Sep 22 Sep 21 Sep 20 Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7 Sep 6 Sep 5 Sep 4
Windows 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
OS X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Linux 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Readme

Source
raw.​githubusercontent.​com

Snippets of Go code for Sublime Text

csv.Read:

r := csv.NewReader(strings.NewReader(${1:in}))

for {
    record, err := r.Read()
    if err == io.EOF {
        break
    }
    if err != nil {
        log.Fatal(err)
    }

    ${2:fmt.Println(record)}
}

csv.Write:

w := csv.NewWriter(${1:os.Stdout})

for _, record := range records {
    if err := w.Write(record); err != nil {
        log.Fatalln("error writing record to csv:", err)
    }
}

w.Flush()

if err := w.Error(); err != nil {
    log.Fatal(err)
}

http.Basic:

func handler${1:route}(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "...")
}

func main() {
    http.HandleFunc("/${1:route}", handler${1:route})
    http.HandleFunc("/", http.NotFound)
    http.ListenAndServe(":8080", nil)
}

http.Get:

resp, err := http.Get("${1:http://example.com/}")
if err != nil {
    ${2:log.Fatal(err)}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    ${3:log.Fatal(err)}
}
${4:// code...}

http.Route:

func handler${1:route}(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "...")
}

func init() {
    http.HandleFunc("/${1:route}", handler${1:route})
}

ioutil.NopCloser:

${1:rc} := ioutil.NopCloser(${2:r})

ioutil.ReadAll:

${1:b}, err := ioutil.ReadAll(${2:r})
if err != nil {
    ${3:log.Fatal(err)}
}

ioutil.ReadDir:

${1:files}, err := ioutil.ReadDir(${2:dir})
if err != nil {
    ${3:log.Fatal(err)}
}

ioutil.ReadFile:

${1:b}, err := ioutil.ReadFile(${2:file})
if err != nil {
    ${3:log.Fatal(err)}
}

ioutil.TempDir:

${1:tmpname}, err := ioutil.TempDir(${2:dir}, ${3:prefix})
if err != nil {
    ${4:log.Fatal(err)}
}

ioutil.TempFile:

${1:f}, err := ioutil.TempFile(${2:dir}, ${3:prefix})
if err != nil {
    ${4:log.Fatal(err)}
}

ioutil.WriteFile:

if err := ioutil.WriteFile(${1:filename}, ${2:data}, ${3:perm}); err != nil {
    ${4:log.Fatal(err)}
}

json.Marshal:

b, err := json.Marshal(${1:data})
if err != nil {
    log.Fatal(err)
}

json.NewDecoder:

var ${1:data} ${2:interface{}}
if err := json.NewDecoder(${3:r}).Decode(&${1:data}); err != nil {
    ${4:log.Fatal(err)}
}

json.NewEncoder:

var ${1:data} ${2:interface{}}
if err := json.NewEncoder(${3:w}).Encode(&${1:data}); err != nil {
    ${4:log.Fatal(err)}
}

json.Unmarshal:

var ${2:dst} ${3:map[string]string}
if err := json.Unmarshal(${1:jsonBlob}, &${2:dst}); err != nil {
    log.Fatal(err)
}

md5.New:

${1:h} := md5.New()
    io.WriteString(${1:h}, ${2:"content"})
    ${3:md5sum} := fmt.Sprintf("%x", md5.Sum(${4:nil}))

md5.Sum:

${1:md5sum} := fmt.Sprintf("%x", md5.Sum(${2:data}))

sha1.New:

${1:h} := sha1.New()
    io.WriteString(${1:h}, ${2:"content"})
    ${3:sha1sum} := fmt.Sprintf("%x", sha1.Sum(${4:nil}))

sha1.Sum:

${1:sha1sum} := fmt.Sprintf("%x", sha1.Sum(${2:data}))

strconv.Atoi:

${1:i}, err := strconv.Atoi(${2:str})
if err != nil {
    log.Fatal(err)
}

strconv.ParseBool:

${1:v}, err := strconv.ParseBool(${2:str})
if err != nil {
    log.Fatal(err)
}

strconv.ParseFloat:

${1:f64}, err := strconv.ParseFloat(${2:str}, ${3:64})
if err != nil {
    log.Fatal(err)
}

strconv.ParseInt:

${1:i64}, err := strconv.ParseInt(${2:str}, ${3:10}, ${4:64})
if err != nil {
    log.Fatal(err)
}

strconv.ParseUint:

${1:u64}, err := strconv.ParseUint(${2:str}, ${3:10}, ${4:64})
if err != nil {
    log.Fatal(err)
}

tabwriter:

w := new(tabwriter.Writer)

// Format in tab-separated columns with a tab stop of 8.
w.Init(${1:os.Stdout}, ${2:0}, ${3:8}, ${4:0}, ${5:'\t'}, ${6:0})
fmt.Fprintln(w, "${7:...\t...}")
fmt.Fprintln(w)

if err := w.Flush(); err != nil {
    log.Fatal(err)
}

time.After:

select {
case <-time.After(${1:5 * time.Minute}):
    ${2:fmt.Println("timed out")}
${3:case m := <-c:
    handle(m)
}
}

time.Date:

${1:t} := time.Date(${2:2009}, time.${3:November}, ${4:10}, ${5:23}, ${6:0}, ${7:0}, ${8:0}, ${9:time.UTC})

time.Parse:

${1:t}, err := time.Parse(${2:`Mon Jan 2 15:04:05 -0700 MST 2006`}, ${3:str})
if err != nil {
    log.Fatal(err)
}

time.ParseDuration:

${1:d}, err := time.ParseDuration(${2:str})
if err != nil {
    log.Fatal(err)
}

time.Tick:

${1:c} := time.Tick(${2:1 * time.Minute})
for now := range ${1:c} {
    ${3:fmt.Printf("%v\n", now)}
}