Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

> So depending on the algorithm you are using, you are going to leave some of these fields in the struct as null/empty?

In Go, when you create a new variable, it's automatically set to the null/zero value. So in this case, they would be nil.



Which is not necessarily the default value you want for them.

So not the same thing with default values at all.

With this struct interface you just implement "optional" values...


For default values, there are at least two choices. The first one I mentioned -- just create a variable and export it. Users can pass it:

In skein package:

   var DefaultHash = &Args{ ... } // the default args you want
Users will call it like this:

   skein.New(32, &skein.DefaultHash)
Another way, better for some cases, is have methods on arguments struct, like in base32 package from stdlib http://golang.org/pkg/encoding/base32/

  base32.StdEncoding.DecodeToString(...)
  base32.HexEncoding.DecodeToString(...)
In most cases (but now all), there are better ways to design API than stuff them with default arguments, which make functions behave unexpectedly if you forget something.

But maybe we should talk about specific cases? What are the examples of functions which need to accept default arguments? Maybe I can try to convert them into something reasonable in Go?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: