On Thu, Mar 31, 2022 at 1:14 PM Adam Pritchard <pritchard.a...@gmail.com>
wrote:

> I’m working on a library to help get the “real” client IP from HTTP
> requests:
> https://github.com/realclientip/realclientip-go
> https://pkg.go.dev/github.com/realclientip/realclientip-go
>
> Right now the “strategies” are like:
>
> type Strategy func(headers http.Header, remoteAddr string) string
> ...
> clientIPStrategy, err := 
> realclientip.RightmostTrustedCountStrategy("X-Forwarded-For", 1)
> ...
> clientIP := clientIPStrategy(req.Header, req.RemoteAddr)
>
> So, functions matching a signature are created that process the input.
>
> But I keep wondering: Should I be returning types (structs) adhering to an
> interface instead?
>
> I’m starting to think I should, but I can’t think of what difference it
> would make.
>

One option is to return interfaces, but at the same time define a function
type that implements that interface:

type Strategy interface {
   GetIP(headers http.Header, remoteAddr string) string
}

type StrategyFunc func(headers http.Header, remoteAddr string) string

func (s StrategyFunc) GetIP(headers http.Header, remoteAddr string) string
{return s(headers,remoteAddr)}

This way, you can use functions of type StrategyFunc as well as interfaces
of type Strategy when you create such strategies. The users of the library
would always see the interface.

This is similar to how the handlers are done in the http library.


> Any feedback would be appreciated.
>
> Adam Pritchard
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/20a8eac9-98b2-4af5-87f3-e811d5a97e3cn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/20a8eac9-98b2-4af5-87f3-e811d5a97e3cn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2Rqo1Mjh3eL7VjV3mHT6owgEnO5iF6%3D36a1WGMEbKpPayWA%40mail.gmail.com.

Reply via email to