https://play.golang.org/p/Lscyy3dKXLh


(Cherolyn Lexvold) #1

https://play.golang.org/p/Lscyy3dKXLh

I missed something. Why does …
fmt.Printf("%T\n", a)
fmt.Printf("%T\n", b)
fmt.Printf("%T\n", c)
…produce…
int
float64
string
…?


(J Ohn Stuart) #2

%T is the type of your variable

a := 1 makes a by default an int, and b := 1.1 make b by default a float64


(Cherolyn Lexvold) #3

I kinda get it but not really.
So, when you say “by default” does this mean something that has been preset or predefined by Go?


(J Ohn Stuart) #4

It is a convention, as far as I know, that 1 is an int and 1.1 is a float64 - see this in the go tutorial about literal data types: https://tour.golang.org/basics/13

what I am not sure is if h:= 1.1 is a float64 on a 64 bit machine and a float 32 on a 32 bit machine. Could anyone help on this one ?


(Ignacio Gómez) #5

From the Go tour:

But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:

i := 42           // int
f := 3.142        // float64
g := 0.867 + 0.5i // complex128

You can confirm this on the playground (which has a 32 bit arch) by seeing that 1.1 is a float64, 1 an int, and int's size is 4 bytes (32 bits): https://play.golang.org/p/zfxE6sauGnD


(Cherolyn Lexvold) #6

The first paragraph is quite helpful.

The second paragraph is interesting


(Cherolyn Lexvold) #7

What does the “//” mean?
Bear with me. I’m like a baby learning to talk. :rofl:


(Norbert Melzer) #8

It starts a comment.


(Cherolyn Lexvold) #9

Interesting. What does “\n” mean?


(Cherolyn Lexvold) #10

So in" i := 42 // int", the “//int” is a commentary on the foregoing? but not a part of the program?


(Jay Ts) #11

Correct. Anything on the line starting with // is ignored by the compiler, to the end of that same line.

https://golang.org/ref/spec#Comments

I know you are just getting started, but I suggest that as soon as you can (and this applies to everyone), get used to looking in the official specification for definitive answers. Compared to other languages, Go’s language specification is surprisingly easy to read! Comments are about the simplest part of any programming language, so it’s a good place to start.


(Jay Ts) #12

It’s a symbol for a “newline”. In “baby talk”, it means that your fmt.Printf() statements result in

int
float64
string

rather than

intfloat64string

Remove the /n’s and try it. :wink:

Suggested reading: http://www.golang-book.com/books/intro

Or if you don’t mind spending a little money, look for his book “Introducing Go” by O’Reilly. It’s a good book for people who are just starting with Go and programming.


(Cherolyn Lexvold) #13

Thanks!


(Cherolyn Lexvold) #14

Very helpful!


(vapoh ) #15

yes this is very helpful
learn go tutorial https://www.welookups.com/go/default.html