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 like 
sizeof in C/C++ *or* it should be the sum of the lengths of its (sequence) 
members.

The first case is way too low-level for Go, because it belongs to an unsafe 
operation so that's an easy no, ... and it already exists as 
unsafe.Sizeof().

The second case (len being the sum of its members' lengths) would require 
recursive calls, and almost surely infinite cycles as we eventually get to 
pointers. 

On Sunday, October 24, 2021 at 8:11:37 PM UTC+2 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?
>
> Cordially,
> Jon Forrest
>
> ----------
> package main
>
> import "fmt"
>
> var s struct {
>         i1      int
>         i2      int
> }
>
> func main() {
>        fmt.Printf("len(s) = %d\n", len(s)) // -- invalid argument s (type 
> struct { i1 int; i2 int }) for len
> }
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a3dff2c4-94ec-40da-ae87-5334bbcd5d94n%40googlegroups.com.

Reply via email to