News for hackers. Stuff that matters.
Re:Google's Go Programming Language Grows Up: Now What? by jeremyjh on Thursday September 20, 2012 @09:52PM (#4551811)
So depending on the algorithm you are using, you are going to leave some of these fields in the struct as null/empty?

To me this is the whole issue. Of course if we have knowledge outside the method declaration about what values can be left 0, then we can pass the correct ones as needed. But the interface is ambiguous about when that is acceptable, and the implementation can change without changing the interface.

According to the mail lists I've read on the subject, the correct way to implement your algorithms in Go would be for each to be a separate method. And indeed that is the only way in Go to support a fully specified interface.

Re:Google's Go Programming Language Grows Up: Now What? by earthboundkid on Thursday September 20, 2012 @11:42PM (#4552087)
> 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.

Re:Google's Go Programming Language Grows Up: Now What? by batista on Friday September 21, 2012 @04:15AM (#4552826)
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...

Re:Google's Go Programming Language Grows Up: Now What? by dchest on Friday September 21, 2012 @05:12AM (#4552934)
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?