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
| " ~/.config/nvim/init.vim | |
| call plug#begin('~/.local/share/nvim/plugged') | |
| Plug 'NLKNguyen/papercolor-theme' " Color scheme | |
| Plug 'fatih/vim-go' " Go development | |
| Plug 'scrooloose/nerdtree' " Sidebar nav | |
| Plug 'ctrlpvim/ctrlp.vim' " Fuzzy finder nav | |
| Plug 'easymotion/vim-easymotion' " In-file nav | |
| " Autocomplete |
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 main() { | |
| http.ListenAndServe(":8080", | |
| logware( | |
| http.HandlerFunc(handle), | |
| ), | |
| ) | |
| } | |
| func handle(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(201) |
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
| type statusRecorder struct { | |
| http.ResponseWriter | |
| status int | |
| } | |
| func (rec *statusRecorder) WriteHeader(code int) { | |
| rec.status = code | |
| rec.ResponseWriter.WriteHeader(code) | |
| } |
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 init() { | |
| rand.Seed(time.Now().UnixNano()) | |
| } | |
| func retry(attempts int, sleep time.Duration, f func() error) error { | |
| if err := f(); err != nil { | |
| if s, ok := err.(stop); ok { | |
| // Return the original error for later checking | |
| return s.error | |
| } |
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
| // DeleteThing attempts to delete a thing. It will try a maximum of three times. | |
| func DeleteThing(id string) error { | |
| // Build the request | |
| req, err := http.NewRequest( | |
| "DELETE", | |
| fmt.Sprintf("https://unreliable-api/things/%s", id), | |
| nil, | |
| ) | |
| if err != nil { | |
| return fmt.Errorf("unable to make request: %s", err) |
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
| // 1. Prints: A | |
| retry(3, time.Second, func() error { | |
| fmt.Print("A") | |
| return nil | |
| }) | |
| // 2. Prints: BB | |
| var i int | |
| retry(3, time.Second, func() error { | |
| i++ |
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 retry(attempts int, sleep time.Duration, fn func() error) error { | |
| if err := fn(); err != nil { | |
| if s, ok := err.(stop); ok { | |
| // Return the original error for later checking | |
| return s.error | |
| } | |
| if attempts--; attempts > 0 { | |
| time.Sleep(sleep) | |
| return retry(attempts, 2*sleep, fn) |