fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.36k stars 619 forks source link

Suggest using method value instead of Print*Func #35

Closed mgeisler closed 8 years ago

mgeisler commented 8 years ago

The Print*Func methods can be replaced with a method value in most cases. Method values are supported since Go 1.1:

https://golang.org/doc/go1.1#method_values

mgeisler commented 8 years ago

Hi @fatih. This is a cut-down version of #34 in case you think it's nice to make people aware of the simpler way to call these functions.

fatih commented 8 years ago

I'm not sure we should dictate person how to use it. No one ever checks the error. I think the current doc is good as it is.

mgeisler commented 8 years ago

Well, it confused me to see the functions in the library since I had just learnt about the method values :-) Maybe there are others that haven't heard of those and who will be using the Print*Func methods unnecessarily. So it's not about checking errors, it's about guiding people towards the best and easiest solution.

fatih commented 8 years ago

@mgeisler can you give a real time example on how people use and how you except to be used? I'll merge then if it makes sense, sorry just trying to understand it :)

mgeisler commented 8 years ago

I guess I'm just imagining that someone would be happy to know that you can do

red := color.Red.Print

instead of

red := color.Red.PrintFunc()

At least for new code where the change in signature isn't a problem. Nothing more than that, really.

fatih commented 8 years ago

Got it, thanks for the explanation. I think the current thing is good. I wish I could change a lot of the API, but the current situation serves well and is used by many other packages. Also note that the signature of red in your examples are different for both cases. PrintFunc() returns a function that doesn't has any return values, wheres Print and co are regular print functions. They stil can use them so it's up to them how to use :)