This file contains 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 (wrapper *ServiceWrapper) GetProductStock(id int) (int, error) { | |
// Get Version 2 | |
if id > 100 { | |
return wrapper.service.GetProductStockV2(id) | |
} | |
// Otherwise, use the old version | |
return wrapper.service.GetProductStock(id) | |
} |
This file contains 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 (wrapper *ServiceWrapper) GetProductStock(id int) (stock int, err error) { | |
key := fmt.Sprintf("key-%d", id) | |
value, err := wrapper.cache.Get(key) | |
if err == nil && value != "" { | |
stock, _ = strconv.Atoi(value) | |
return | |
} | |
defer func () { | |
_ = wrapper.cache.Set(key, strconv.Itoa(stock)) |
This file contains 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 ( | |
"net/http" | |
"time" | |
) | |
func main() { | |
httpClient := &http.Client{Timeout: time.Duration(3) * time.Second} | |
cacheService := NewCache() | |
service := NewService(httpClient, cacheService) | |
This file contains 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 formatter | |
// golangJSONItf interface implements all the functions of GolangJSON. | |
type golangJSONItf interface { | |
Marshal(v interface{}) ([]byte, error) | |
} | |
// Formatter uses local json library wrapper rather than encoding/json. | |
type Formatter struct { | |
golangJSON golangJSONItf |
This file contains 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 "errors" | |
type WarehouseMock struct {} | |
func(warehouse *WarehouseMock) GetProduct(id int) (Product, error){ | |
if id != 1 { | |
return Product{}, errors.New("test") | |
} | |
return Product{}, nil | |
} |
This file contains 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 WarehouseItf interface { | |
GetProduct(id int) (Product, error) | |
InsertProduct(product Product) error | |
} | |
type Retail struct { | |
warehouse WarehouseItf | |
} | |
func (retail *Retail) GetMyProduct() ([]Product, error) { |
This file contains 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 generalError struct { | |
text string | |
code CodeStatus | |
} | |
func (error *generalError) Error() string { | |
return fmt.Sprintf("%s %d", error.text, error.code) | |
} | |
func NewError(text string) error { |
This file contains 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
db, err := sql.Open() | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer func (){ | |
if errDB := db.Close(); errDB != nil { | |
log.Fatal(errDB) | |
} | |
} |
This file contains 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
db, err := sql.Open() | |
if err != nil { | |
log.Fatal(err) | |
} | |
tx, err := db.Begin() | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer func() { |
This file contains 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 Wallet struct { | |
money int | |
} | |
func (wallet *Wallet) MyMoney() { | |
fmt.Println("$", wallet.money) | |
} | |
func (wallet *Wallet) AddMoney(money int) { | |
wallet.money += money |