Created
August 31, 2023 09:51
-
-
Save mortymacs/dd3c2025b3b54fa38a1a99933a788191 to your computer and use it in GitHub Desktop.
sample method chain
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 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