Live data from Hacker News

Go Data Structures: Interfaces (2009)

research.swtch.com

1–10 of 80 posts

Re: Go Data Structures: Interfaces (2009)

#4
post #2

A quirk of Go is that I can cast a `[]string` to `interface{}`, but I cannot cast `[]string` to `[]interface{}`. This blog post is my go-to explanation for why the second is not possible but the former is.

Is that really a "quirk" of specifically Go? I am pretty sure Java--the implementation of which defined a lot of how numerous languages handle this kind of thing--has the same behavior: you can cast String[] to Object, but not Object[].

Re: Go Data Structures: Interfaces (2009)

#5
post #2

A quirk of Go is that I can cast a `[]string` to `interface{}`, but I cannot cast `[]string` to `[]interface{}`. This blog post is my go-to explanation for why the second is not possible but the former is.

> I can cast a `[]string` to `interface{}`, but I cannot cast `[]string` to `[]interface{}`

Surely that's just contravariance, though? You can't cast []string to []any because that would allow you to write non-strings to it.

Re: Go Data Structures: Interfaces (2009)

#6
post #4
post #2

A quirk of Go is that I can cast a `[]string` to `interface{}`, but I cannot cast `[]string` to `[]interface{}`. This blog post is my go-to explanation for why the second is not possible but the former is.

Is that really a "quirk" of specifically Go? I am pretty sure Java--the implementation of which defined a lot of how numerous languages handle this kind of thing--has the same behavior: you can cast String[] to Object, but not Object[].

C# allows casting string[] to object[], but this requires the compiler to insert runtime type checks each time the array elements are modified:

  object[] array = new string[10];  
  // throws a run-time exception 
  array[0] = 10;

Re: Go Data Structures: Interfaces (2009)

#7
If you are mindful of performance, one thing to be careful of, is that interfaces often prevent inlining and can cause performance issues.

Generally speaking the Go compiler seems to have trouble with inlining, PGO (profile guided optimization) can help though.

Re: Go Data Structures: Interfaces (2009)

#9
post #7

If you are mindful of performance, one thing to be careful of, is that interfaces often prevent inlining and can cause performance issues. Generally speaking the Go compiler seems to have trouble with inlining, PGO (profile guided optimization) can help though.

> Go compiler seems to have trouble with inlining

not "have trouble", a deliberate design to keep compile times fast and (emitted) code size reasonable

Re: Go Data Structures: Interfaces (2009)

#10
post #7

If you are mindful of performance, one thing to be careful of, is that interfaces often prevent inlining and can cause performance issues. Generally speaking the Go compiler seems to have trouble with inlining, PGO (profile guided optimization) can help though.

Storing concrete values into interfaces often cause allocations, which has a larger impact on performance than inlining.
Post reply on HN