This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Go questions...

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • 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
  • 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;-)
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!