Golang: How do the functions len () and make () work?

How do the functions len() and make() ? Since the language does not have support for both generics and the overload function, I do not see the possibility of func len(v Type) int . The same goes for func make(Type, size IntegerType) Type .

I cannot find the function in the go source, the closest I managed to find is this

+5
source share
2 answers

The len and make functions are part of the language specification and are built into the compiler. Support for built-in functions Runtime is supported in the runtime package .

The builtin.go file is used for documentation only. It is not compiled.

+9
source

Due to the strict Go types, the compiler always knows which type you pass to the len function, and therefore it moves on to another function for different types, which can be determined at compile time. In most cases, you try to get the slice length, in which case the len function should only return the len field for this slice structure (since the slice is really a structure); same for string.

Compilers have all kinds of tricks, the assembly code generated by the compiler rarely follows the same logic that you typed.

+1
source

Source: https://habr.com/ru/post/1212153/


All Articles