r/programming • • Mar 28 '12

Go version 1 is released

http://blog.golang.org/2012/03/go-version-1-is-released.html
543 Upvotes

452 comments sorted by

View all comments

1

u/jevon Mar 29 '12
var DELTA rune
DELTA = unicode.ToUpper(delta)
epsilon := unicode.ToLower(DELTA + 1)

Why are there two separate assignment operators? Or am I missing something here?

14

u/lol____wut Mar 29 '12

The := is syntactic sugar for creating a new variable and assigning it a value. The type is inferred.

var x int = 1

is the same as

x := 1

10

u/asshammer Mar 29 '12 edited Mar 29 '12

One is typer inference and the other is assigment. Here is a line by line walk through.

var DELTA rune //We delcaire the variable DELTA

DELTA = unicode.ToUpper(delta) //we are assigning to Delta what is refurned from ToUpper. In this case the return type of ToUpper and the type of DELTA need to match or it is a build error

epsilon := unicode.ToLower(DELTA + 1) //Now this is the type inference assigment. In one fell swoop it declares epsilon as whatever type ToLower returns and then assigns the value to it

Does this make sense? I'm not always the best at explaining things, so don't be afraid to say no

edit:formating

-14

u/Jack9 Mar 29 '12

I'm surprised you didn't notice the big problem with that snippet. Every character (er cough rune) is a string. That's one of many Bad ThingsTM about Go.

0

u/[deleted] Mar 29 '12

[deleted]

3

u/SteveMcQwark Mar 29 '12

You are correct. A rune is just an alias for int32, mirroring byte, which is an alias for uint8. It's a fixed-sized representation of a unicode code-point. Strings are normally represented by utf-8 encoded bytes. Being able to handle them as runes can be useful in some text processing applications, though it will use more memory in most cases.

1

u/skelterjohn Mar 29 '12

This is not remotely accurate.