File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Languages and the fly likes Go questions... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Go questions..." Watch "Go questions..." New topic

Go questions...

atul khot
Ranch Hand

Joined: Jul 24, 2008
Posts: 64
I come from C/C++/Java background - and some awk/perl/ruby too -
decided to give Go a go ;-)
Just started with the gotour - and am loving Go - looks kind of crisp and starched...
Some questions:

1. Why the var keyword - There is nothing like val (to indicate
immutability)? So why not just drop it...

2. Does Go have/supports global variables...
(Global as in C)


Constraining the visibility/scope of variables is a big
theme - (File level "static" in C/C++, package variable in java)
How does Go go with globals ;-)
Or they are kind of package variables? Or field variables
(as in Java) - package being a namespace?

3. Type inference: What is the rationale for having type
inference with := (within a function) - and not outside?
Why not provide for type inference everywhere?

4. Why to have
var z []int = []int{1,2,3}
and not
var z int [] = int[]{1,2,3}

5. Not a question - case/fallthrough is nice ;-)

--cheerio atul
Mark Summerfield
Ranch Hand

Joined: Jun 20, 2012
Posts: 37


1. I suspect that var was originally created to make the language more orthogonal: func to define a function, const to declare a const, and so var to declare a variable. My guess is that := came along later in the the language's development and because it can only be used in particular circumstances (and also to preserve orthogonality), var has been kept.

2. C and C++ can have globals by having a static value in one module (file) that is extern in the other modules (files) that want to use it. In Go all values belong to a particular package (namespace). If you declare a value in a package outside of any function that value can be accessed from any other packages that import it---providing it is exported (starts with an uppercase letter). So, yes, you can have globals, i.e., exported package variables. Note that in Go a single package is made up of any number of files with the same package declaration. Here is an example:

3. Why is := limited to functions? Well, although x := y is shorthand for var x = y, that isn't the whole story about :=. In particular := can be used to redeclare variables, something that var can't do. See Short Variable Declarations.

4. Go's declaration syntax can seem strange to C/C++ programmers. The rationale is given here:
Go's Declaration Syntax.

5. Yes, case/fallthrough is very nice. Although the thing I most like about this is that cases can be arbitrary values (e.g., strings).

Incidentally, the Go specification is in plain English (although my book is the best place to start of course;-)

Mark Summerfield
"Programming in Go" -
I agree. Here's the link:
subject: Go questions...
It's not a secret anymore!