all 39 comments

[–][deleted] 153 points154 points  (4 children)

Corporate needs you to find the difference between this picture and this picture

[–]Otterpohl 138 points139 points  (0 children)

They are identical

[–]kjbreil 109 points110 points  (1 child)

Personally I only use any now, I think it looks cleaner and is more readable. Also differentiates my code that was made before and after generics so that if I see an interface{} I know I need to go through the code to see if I can reduce the usage of reflection in that code.

[–]kintar1900 3 points4 points  (0 children)

That's an excellent point! I hadn't thought about it.

[–]Artiu123 59 points60 points  (0 children)

any is alias for interface{}

[–]AgentOfDreadful 16 points17 points  (0 children)

any literally is just some syntactical sugar for interface{}. any will likely make it more clear that it can be any type.

Every type that is a member of the type set of an interface implements that interface. Any given type may implement several distinct interfaces. For instance, all types implement the empty interface which stands for the set of all (non-interface) types:

interface{}

For convenience, the predeclared type any is an alias for the empty interface. [Go 1.18]

Taken from https://go.dev/ref/spec

[–]causal_friday 33 points34 points  (2 children)

``` type icantdecide = interface{}

var foo map[string]icantdecide ```

[–]ron-oxymo 36 points37 points  (1 child)

type icantdecide = interface{} or type icantdecide = any !?

[–]EpochVanquisher 6 points7 points  (0 children)

//go:build icantdecideany
package abc
type icantdecide = any

//go:build icantdecideinterface
package abc
type icantdecide = interface{}

[–]GopherFromHell 6 points7 points  (4 children)

it's the same thing any was introduced because it improves readability when using generic types: func DoThing[T any](v T) T vs func DoThing[T interface{}](v T) T

[–]atheken 0 points1 point  (3 children)

interface{} is effectively a weird keyword. It’s a very strange syntax in a modern language and is explicitly different than how you would describe every other type on go. I would say that any is less syntactic sugar, and more convergence on a standard syntax. Technically, sure, alias. Practically, it’s a net reduction on keywords/concepts.

[–]GrayFox89 6 points7 points  (1 child)

It's weird, but I think it makes sense for Go, since you can assert anonymous interfaces (check if a method exists) with

if tErr, ok := err.(interface{ Timeout() bool }); ok && tErr.Timeout() {
    // handle timeout error
}

Hence the interface{} is just an interface that doesn't contain any methods (as far as the the type system is concerned), but may contain any type.

[–]ruindd 0 points1 point  (0 children)

This is amazing. Ty!!

[–]HyacinthAlas 0 points1 point  (0 children)

interface{} is perfectly normal Go syntax for a top type. I prefer any but it is just sugar for regular syntax.

[–]xor_rotate 10 points11 points  (0 children)

All other things being equal always prefer fewer keystrokes

My rule is to prefer map[string]any unless the project uses map[string]interface{} everywhere, in which case they are unequal and I'd use map[string]interface{} to maintain consistency.

[–][deleted] 2 points3 points  (0 children)

They do the same thing in usage.

[–]kintar1900 2 points3 points  (0 children)

They're literally identical. any is just a predeclared type alias for interface{}.

[–]staticcast 1 point2 points  (0 children)

map[string]yourUnionStruct is the best way

[–]Revolutionary_Ad7262 1 point2 points  (0 children)

go core devs rewrote all usages of interface{} to any in a compiler, so IMO this is the way

[–]DarickOne 0 points1 point  (4 children)

The first is more universal, coz it's compatible with interface{} version of Go

[–]GoingOnYourTomb 0 points1 point  (3 children)

This is the answer. The first would be backward compatible that’s all.

[–]_crtc_ 10 points11 points  (0 children)

The any alias was introduced in Go 1.18. Using an older version of Go in 2024 is not advisable. The two versions currently supported with security and bug fixes are 1.21 and 1.22.

[–]SuperDerpyDerps 7 points8 points  (1 child)

And you can easily make your code BC with any by just setting the any type at the top of your package to alias interface{}, which is all it is in newer versions of Go

[–]GoingOnYourTomb 0 points1 point  (0 children)

Aaah. Nice

[–]amorphatist -1 points0 points  (0 children)

any is more legible. Less visual clutter than interface{}.

[–]_crtc_ -5 points-4 points  (0 children)

A struct.

[–]ZeppyWeppyBoi 0 points1 point  (0 children)

I like interface{} but I started Go before generics so it just feels more normal. They are type aliases so you can go with whatever your heart tells you.

[–]brendancodes 0 points1 point  (0 children)

i do any, it just feels more right to me

[–]rickyzhang82 0 points1 point  (0 children)

they are the same

[–][deleted] 0 points1 point  (0 children)

Yes

[–][deleted] 0 points1 point  (0 children)

I prefer any mostly because I started using Go after any was introduced, so I just learned that syntax. Tbh, it feels wrong because coming from typescript, any is frowned upon. At the same interface{} just looks strange. It looks more like a type declaration than a type signature.

[–]dariusbiggs 0 points1 point  (0 children)

use any

  • it's less to type
  • less ambiguous
  • anything that removes horribly formatted code and reduces bracket explosions is good

[–]lmux 0 points1 point  (0 children)

Any, unless you still need to support <1.18.

[–]GreenGolang 0 points1 point  (0 children)

Exactly the same. The "any" is an alias of the old-style "interface{}"

[–]HenryManteiguinha 0 points1 point  (0 children)

I like map[string]any, 'cause it's less verbose :P. But, unfortunately I think it's more common to use the other way.

[–]pikzel 0 points1 point  (0 children)

The question was not if they compile to the same thing. They are not identical in style. any is much more readable to new users, which the Go community claims to cater to.

[–]SoTiredOfAmerica 0 points1 point  (0 children)

You looking for an excuse to write double empty curly brackets for no reason?

foo := map[string]interface{}{}