• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Go questions...

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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)


works...

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 ;-)
 
author
Posts: 37
VI Editor Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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;-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic