Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment operator in Go language

Tags:

syntax

go

Lately I was playing with google's new programming language Go

and was wondering why the assignment operator := has a colon in front of the equal sign = Is there a particular reason why the authors of the language wanted to use name := "John" instead of name = "John"

like image 212
Nenad Avatar asked May 13 '13 11:05

Nenad


People also ask

What does += operator mean in Golang?

+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A. -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand.

What does <- mean in go?

"=" is assignment,just like other language. <- is a operator only work with channel,it means put or get a message from a channel. channel is an important concept in go,especially in concurrent programming.

Does Go support operator overloading?

Since Go does not support operator overloading, ordinary functions must be used to manipulate user types.

How will you classify the Go programming language in the context of typed language?

Go is a general-purpose language designed with systems programming in mind. It is strongly typed and garbage-collected and has explicit support for concurrent programming.


2 Answers

The := notation serves both as a declaration and as initialization.

foo := "bar" 

is equivalent to

var foo = "bar" 

Why not using only foo = "bar" like in any scripting language, you may ask ? Well, that's to avoid typos.

foo = "bar" fooo = "baz" + foo + "baz"   // Oops, is fooo a new variable or did I mean 'foo' ? 
like image 172
Fabien Avatar answered Dec 22 '22 00:12

Fabien


name := "John" 

is just syntactic sugar for

var name string name = "John" 

Go is statically typed, so you have to declare variables.

like image 42
Joonazan Avatar answered Dec 22 '22 00:12

Joonazan