Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created December 2, 2020 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/9b8d0eb891139a4c4ad2692cc6b9cf45 to your computer and use it in GitHub Desktop.
Save podhmo/9b8d0eb891139a4c4ad2692cc6b9cf45 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Service struct {
BasePath string
gw *gateway
Team *TeamService
User *UserService
}
func (s *Service) Do(method string, path string) (string, error) {
return fmt.Sprintf("%s: request %s%s", method, s.BasePath, path), nil
}
func New() *Service {
s := &Service{BasePath: "/api"}
gw := &gateway{Service: s}
s.gw = gw
s.Team = (*TeamService)(gw)
s.User = (*UserService)(gw)
return s
}
type gateway struct {
Service *Service
}
type TeamService gateway
func (s *TeamService) List() (string, error) {
return s.Service.Do("GET", "/team")
}
type UserService gateway
func (s *UserService) List() (string, error) {
return s.Service.Do("GET", "/user")
}
func main() {
s := New()
fmt.Println(s.Team.List())
fmt.Println(s.User.List())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment