Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created August 31, 2023 09:51
Show Gist options
  • Save mortymacs/dd3c2025b3b54fa38a1a99933a788191 to your computer and use it in GitHub Desktop.
Save mortymacs/dd3c2025b3b54fa38a1a99933a788191 to your computer and use it in GitHub Desktop.
sample method chain
package main
import "fmt"
type Client struct {
URL string
// Methods
User *User
Post *Post
}
type User struct {
Client *Client
}
type Post struct {
Client *Client
}
func New(URL string) *Client {
c := &Client{URL: URL}
// Initialize methods.
c.User = &User{Client: c}
c.Post = &Post{Client: c}
return c
}
func (u *User) Create(arg string) error {
fmt.Println("user create called", arg)
return nil
}
func (p *Post) Publish(isDraft bool) error {
fmt.Println("post published with isDraft:", isDraft)
return nil
}
func main() {
c := New("http://localhost")
c.User.Create("test")
c.Post.Publish(true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment