What's wrong with this?

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

You are missing a closing quote:

	fmt.Printf ("%T\n", x)

Oh :roll_eyes: :sweat_smile:

How about this?
https://play.golang.org/p/oNiZuLM5PXw

1 Like

Looking at the error message can be helpful in these cases; it gives you the specific line with the problem and a brief explanation. In this case: “prog.go:8:7: syntax error: non-declaration statement outside function body”

y int = 43 is not a valid statement in Go. I think you’re looking for var y = 43

Edit: This is a bad answer, please see @framp’s answer below (I’m keeping this here for the sake of keeping the context/thread history)

That works :slightly_smiling_face:

Try https://play.golang.org/p/QSSrKwFPwvr

1 Like

Interesting. Why did that work, while mine didn’t? Was it a tab?

Note the syntax differences between your version:

const x = 42
      y int = 43

And @framp’s version:

const (
	x     = 42
	y int = 43
)

(Also note that my original answer up above was bad as it did not declare a const, which is what you seem to be trying to do, and @framp’s answer is much more suitable)

This is not a tab issue; const x = 42 declares a single constant. Following the const keyword with parentheses allows you to declare multiple constants at once without writing const in front of each one. An alternative way you could write this is

const x = 42
const y int = 43

Using

const (
	x     = 42
	y int = 43
)

Is just another way of writing that without writing the const keyword for each item.

1 Like

Cool! Thank you so much! I didn’t notice the () :blush:

Parenthesis can be used that way in either var or const declarations, and also with type declarations and import declarations.

Go allows flexibility in declarations to keep things concise. If you were declaring x and y as variables rather than constants, any of the following would also work:

var x int = 42
var y int = 43

or

var x = 42
var y = 43

The int in the declaration for y is optional since initializing it to an integer (43, rather than 43.0 or "43") provides the information that y is an integer. If you don’t want to initialize the values yet, you can specify just the types:

var x int
var y int

This just declares both as integers, and they are automatically initialized to 0 (zero). You can then set them to 42 and 43 in main(), init(), or some other function. Using parenthesis,

var (
    x = 42
    y = 43
)

This is just like the example shown by the other folks, and you can use the variations shown above within the parenthesis.

There is another way to declare and initialize variables. It works only inside functions:

func main() {
    x := 42
    y := 43
}

This last method declares the variable and initializes it at the same time. It’s more concise, but then the variables are accessible only within that particular function or block.

Finally, it’s a very simple place to use it, but when declaring constants, you can use iota to declare x and y, and set them to 42 and 43:

const (
    x = iota + 42
    y
 )

iota is really useful when declaring many consts with related values, so pay attention to iota while you are learning about declarations. :wink:

1 Like

Just give it more pay attention

Thanks so much for your †ime. ˇhis was very helpful

Ok

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.