{

Golang Map type tutorials with examples


Some programming languages allow you to declare multiple variables with a single line.

This post, show you how to declare multiple variables.

In Golang, Variables are declared as follows

var variable type

How to declare multiple variables of the same type in Golang?

Is it possible to declare multiple variables in Golang?

Yes, It is possible to declare multiple variables of the same type in a single statement.

Here is a declaration syntax for multiple variables.

var v1, v1, v3 string;

Three variables are declared v1,v2,v3 with string type.

How to declare multiple variables of a different type in Golang?

And It gives an error to declare multiple variables of different types with the type declaration.

You can also declare and assign variables

Here is code to declare and assign multiple variables.

    v1, v2 := "one", "two"; 

The above type is removed because of the initial value.

You can declare and assign multipe variables of different types.

var num, str = 12, "one"
fmt.Println(num, str) // 12 one

Conclusion

In golang, You can declare multiple variables of the same type, does not accept multiple variables different types with the type declaration.

Still, you can declare multiple variables of different types with an initial value, without type.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.