News for hackers. Stuff that matters.
Re:Go’s Features of Last Resort by ben_jones on Friday November 22, 2019 @10:33AM (#21606450)
Two valid use cases I've found for interface{}:

Handling large arbitrary JSON objects where I need to operate on their keys but don't need to work with their values by unmarshaling to map[string]interface{}. The alternative would likely be code generation of struct definitions with JSON bindings, which is likely preferable if you already have code generation in your project, but can be an overhead in smaller ones.

It can also be used to create your own syntactic wrappers around API's which consume interface{} such as encoding/json and database/sql:

  Unmarshal(r *http.Response, dst interface{}) ([]byte, error)

and

  BulkInsert(tableName string, columns []string, values [][]interface{}) (time.Duration, error)
Re:Go’s Features of Last Resort by stevelosh on Friday November 22, 2019 @03:01PM (#21609457)
> Handling large arbitrary JSON objects where I need to operate on their keys but don't need to work with their values by unmarshaling to map[string]interface{}.

If you just need the keys, you should unmarshal to `map[string]json.RawMessage` to avoid the overhead of parsing the values into objects only to throw them away without ever using them.

Re:Go’s Features of Last Resort by cle on Friday November 22, 2019 @10:46AM (#21606588)
Handling JSON is the one place where I’ve used interface{} and not felt dirty. Sometimes you really do need to model “anything” or “whatever”, especially when passing around arbitrary data without making assumptions about its schema.
Re:Go’s Features of Last Resort by RSZC on Friday November 22, 2019 @12:27PM (#21607727)
I use it for shutdown channels. It doesn't matter what you're putting in the channel if you're never putting anything in the channel
Re:Go’s Features of Last Resort by zeeboo on Friday November 22, 2019 @12:51PM (#21608017)
Using chan struct{} is more efficient, and guarantees there is nothing being put into the channel. That's why you see it used on the Context type's Done method, for example.

https://golang.org/pkg/context/#Context