Skip to content

Instantly share code, notes, and snippets.

View ilmiawan's full-sized avatar

muslim.ilmiawan ilmiawan

  • bandung
View GitHub Profile
@ilmiawan
ilmiawan / userClient.go
Last active January 2, 2021 05:56
contoh client
package main
type client struct {
httpClient *http.Client
address string
}
// Client interface
type Client interface {
FetchUserDetailsByID(id int) (DetailsResponse, error)
@ilmiawan
ilmiawan / userServices.go
Last active January 2, 2021 05:51
Contoh service
package main
type service struct {
client Client
}
// Service interface
type Service interface {
GetUserDetailsByUserID(id int) (DetailsResponse, error)
}
package main
// UserDetails Object
type UserDetails struct {
ID int `json:"userId"`
Name string `json:"name"`
Email string `json:"email"`
DateOfBirth string `json:"dateOfBirth"`
PhoneNumber string `json:"phoneNumber"`
Gender string `json:"gender"`
package main
func main() {
userClient := NewClient(httpClient, "http://userservice")
userSvc := NewService(userClient)
user, err := GetUserDetailsByUserID(1)
if err != nil {
logrus.Errorf("[ERROR] failed fetching user details: %v", err)
}
@ilmiawan
ilmiawan / lib.go
Last active January 2, 2021 04:38
contoh fungsi umum
// GetCache to get data from cache
func GetCache(key string, result interface{}) {
res, err := cache.Get(key)
if err != nil {
log.Errorf("[ERROR] failed fetching cache: %v", err)
return
}
err = json.Unmarshal(res, result)
if err != nil {
@ilmiawan
ilmiawan / genericParam.go
Last active January 2, 2021 04:40
contoh generic parameter
func GetCache(key string, result interface{}) {
res, err := cache.Get(key)
if err != nil {
log.Errorf("[ERROR] failed fetching cache: %v", err)
return
}
err = json.Unmarshal(res, result)
if err != nil {
log.Errorf("[ERROR] failed parsing cache : %v", err)
@ilmiawan
ilmiawan / userServicePolymorphism.go
Created January 1, 2021 03:37
contoh polymorphism
// Service interface
type Service interface {
GetUserDetailsByUserID(id int) (DetailsResponse, error)
}
// service
type service struct {
client Client
}
@ilmiawan
ilmiawan / userService.go
Created January 1, 2021 03:17
contoh object constructor
type service struct {
client Client
}
// Service interface
type Service interface {
GetUserDetailsByUserID(id int) (DetailsResponse, error)
}
// NewService initialize function
@ilmiawan
ilmiawan / user.go
Last active January 1, 2021 03:18
Contoh struct
package pengguna
type User struct {
id int
name string
age int
gender string
email string
password string
}