posts-go/posts/2022-07-31-experiment-go.md

78 lines
1.8 KiB
Markdown
Raw Normal View History

2022-07-31 17:10:50 +00:00
# Experiment Go
2023-08-05 18:56:25 +00:00
There come a time when you need to experiment new things, new style, new
approach. So this post serves as it is named.
2022-07-31 17:10:50 +00:00
2023-09-24 12:17:11 +00:00
## Design API by trimming down the interface/struct or whatever
2022-07-31 17:10:50 +00:00
Instead of:
```go
type Client interface {
GetUser()
AddUser()
GetAccount()
RemoveAccount()
}
// c is Client
c.GetUser()
c.RemoveAccount()
```
Try:
```go
type Client struct {
User ClientUser
Account ClientAccount
}
type ClientUser interface {
Get()
Add()
}
type ClientAccount interface {
Get()
Remove()
}
// c is Client
c.User.Get()
c.Account.Remove()
```
The difference is `c.GetUser()` -> `c.User.Get()`.
2023-08-05 18:56:25 +00:00
For example we have client which connect to bank. There are many functions like
`GetUser`, `GetTransaction`, `VerifyAccount`, ... So split big client to many
children, each child handle single aspect, like user or transaction.
2022-07-31 17:10:50 +00:00
2023-08-05 18:56:25 +00:00
My concert is we replace an interface with a struct which contains multiple
interfaces aka children. I don't know if this is the right call.
2022-07-31 17:10:50 +00:00
This pattern is used by [google/go-github](https://github.com/google/go-github).
2022-11-20 08:49:08 +00:00
## Find alternative to [grpc/grpc-go](https://github.com/grpc/grpc-go)
Why?
[See for yourself](https://github.com/grpc/grpc-go/issues?q=is%3Aissue+compatibility+is%3Aclosed).
2023-02-08 08:24:01 +00:00
Also read:
2023-08-05 18:56:25 +00:00
- [A new Go API for Protocol Buffers](https://go.dev/blog/protobuf-apiv2) to
know why `v1.20.0` is `v2`.
2023-02-08 08:24:01 +00:00
- [Go Protobuf Plugin Versioning](https://jbrandhorst.com/post/plugin-versioning/).
2022-11-20 08:49:08 +00:00
Currently there are some:
2022-11-20 08:49:08 +00:00
2023-08-05 18:56:25 +00:00
- [bufbuild/connect-go](https://github.com/bufbuild/connect-go). Comming from
buf, trust worthy but need time to make it match feature parity with grpc-go.
2022-11-20 08:49:08 +00:00
- [twitchtv/twirp](https://github.com/twitchtv/twirp)
- [storj/drpc](https://github.com/storj/drpc)
2022-11-20 08:49:08 +00:00
2022-07-31 17:10:50 +00:00
# Thanks
- [API Clients for Humans](https://blog.gopheracademy.com/advent-2019/api-clients-humans/)