Re: [go-nuts] Why Doesn't "len()" Work With Structs?

2021-10-24 Thread Jan Mercl
On Sun, Oct 24, 2021 at 8:11 PM jlfo...@berkeley.edu wrote: > I noticed that the len() function doesn't take a struct as an argument (see > below). > This is a big surprise. Can someone shed some light on why this restriction > exists? What is the expected value of len() when applied to a

[go-nuts] Re: [ANN] sorty

2021-10-24 Thread jfcg...@gmail.com
Hi, sorty v2.0 has been released with greatly simplified API. It is now possible to sort same underlying native slices (like []int, []MyInt or IntSlice) without a cast. Best.. On Tuesday, October 5, 2021 at 12:52:04 AM UTC+3 jfcg...@gmail.com

Re: [go-nuts] Re: Why Doesn't "len()" Work With Structs?

2021-10-24 Thread 'Dan Kortschak' via golang-nuts
You can use len to determine the sizeof a struct. https://play.golang.org/p/f0x8p_04lP1 ;) On Sun, 2021-10-24 at 16:44 -0700, jlfo...@berkeley.edu wrote: > Thanks for the replies. > > I had been trying to translate the trick from C into Go where you can > find how many structures are in an

Re: [go-nuts] Re: Why Doesn't "len()" Work With Structs?

2021-10-24 Thread jlfo...@berkeley.edu
I'm now aware that that I could use len(([unsafe.Sizeof(T{})][0]byte{})) But this seems overly complicated. Plus, I don't see why this is unsafe. Please advise. Jon On Sunday, October 24, 2021 at 5:14:44 PM UTC-7 kortschak wrote: > You can use len to determine the sizeof a struct. > >

[go-nuts] Why Doesn't "len()" Work With Structs?

2021-10-24 Thread jlfo...@berkeley.edu
I noticed that the len() function doesn't take a struct as an argument (see below). This is a big surprise. Can someone shed some light on why this restriction exists? Cordially, Jon Forrest -- package main import "fmt" var s struct { i1 int i2 int } func

Re: [go-nuts] Re: Why Doesn't "len()" Work With Structs?

2021-10-24 Thread 'Dan Kortschak' via golang-nuts
On Sun, 2021-10-24 at 17:53 -0700, jlfo...@berkeley.edu wrote: > But this seems overly complicated. Sorry. That was a joke. You asked why you can't use len(); you can, by using it to it into create something that is indexable (and so a possible parameter for len()). > Plus, I don't see why this

[go-nuts] Re: Why Doesn't "len()" Work With Structs?

2021-10-24 Thread Filip Dimitrovski
len() works on indexed types - arrays, slices, maps, strings. It also works on buffered channels but we can consider that a sequence. I don't consider a struct a sequence. It's non-obvious what the behaviour would be. Both of these sound reasonable: len() should be the memory size of the struct

[go-nuts] Re: Why Doesn't "len()" Work With Structs?

2021-10-24 Thread jlfo...@berkeley.edu
Thanks for the replies. I had been trying to translate the trick from C into Go where you can find how many structures are in an initialized array of structures by dividing the size of the array by the size of one structure. As I've learned, not only isn't this possible, because you can't get