I think computing memory use of any *constant* is not straightforward and probably not worth it. Even a typed constant may only exist in an instruction stream. For instance "var x int64 = 42" may compile down to a single instruction (or more, depending on the underlying architecture). Or there may be a "constant pool" for larger constants such as strings and structs or arrays.
> On Jan 30, 2019, at 6:55 PM, 伊藤和也 <kazya.ito.dr...@gmail.com> wrote: > > OK, I understand it somehow but I want to make sure my understanding is > correct. > > There are two types of constant "untyped constant" and "typed constant". > > 1. An untyped constant has a size but it's not specific which means an > untyped constant takes some memory. > fmt.Println(100) > > 2. A typed constant has a specific size which means a typed constant takes a > specific amount of memory. > fmt.Println(int(100)) > > Is my understanding correct? > > 2019年1月31日木曜日 9時05分32秒 UTC+9 伊藤和也: > An interger constant is "int" type and takes "8" bytes memory on 64-bit > system. > > fmt.Println(unsafe.Sizeof(100)) // 8 > fmt.Println(reflect.TypeOf(100)) // int > > and an "int32" type value takes "4" bytes. > > var num int32 > fmt.Println(unsafe.Sizeof(num)) // 4 > > So in this case below, Is the memory usage "12" bytes in total? (Question 1) > > var num int32 = 100 > | | > 4 bytes + 8 bytes = 12 bytes > > and in this case below, Is the memory usage "16" bytes in total? (Question 2) > because the integer constant "100" is "8" bytes first then it's converted to > "int32" which is "4" bytes. > > var num int32 = int32(100) > | | | > 4 bytes + 4 bytes + 8 bytes = 16 bytes > > -- > 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 > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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. For more options, visit https://groups.google.com/d/optout.