While the Go port on Windows is not as full featured as it is for the Linux(es) or MacOS, it is still very useful to try it out on a Windows machine. In fact, for the Go language to become more and more popular, it would be very helpful if more people become involved with the Windows Go development community.
To install Go on Windows, you must:
1.) Download the binaries from the Go MinGW Builds page.
2.) Extract the binaries to a location that you can remember or that you will copy onto a clipboard. The folder containing the binaries will be called go. This go directory will contain various subdirectories (sub folders), including a subdirectory called bin, which holds the binaries (including the compiler, 8g, and the linker, 8l)
3.) Next, you should set up the various environment variables. On Windows 7, this would involve going to Start --> Hover over Computer and Right-Click --> Go to Properties
Once you're at the Properties, you go to Advanced System Settings, then click on Environment Variables.
4.) In the Environment Variables, add the following SYSTEM environment variables (by clicking on "New" for each of them, putting the name of the variable (in bold) and then putting the value specified (in italics):
- GOBIN C:\directory you put the go binaries in\go\bin
- GOARCH 386
- GOOS windows
- GOROOT C:\directory you put the go binaries in \go
Now, in order for the command line compilation to work, you have to add the GOBIN to your PATH environment variable.
To do this, find Path in the system environment variables. Edit it. Go to the end of the Path and add a semicolon, followed by %GOBIN% thusly:
somestuff;%GOBIN%
The %GOBIN% will resolve the place where you put the Go binaries, as you specified in another environment variable. This is what the percentage signs are for. They resolve the environment variable to the directory you put the binaries in. Make sure to save your changes appropriately at each step.
5.) Try out Go in a command line now. You should be able to write a source file and then try using the 8g and 8l commands. For example, write a small program in Notepad, save it as test1.go and put it where you'll remember it.
Then,
C:\workdir>8g test1.go
C:\workdir>8l test1.8
Then, you should be able to start the new executable, which if you didn't change the name using the -o option, should be:
C:\workdir>8.out.exe
Try it!
This is a blog that is associated with my website, www.goprogrammingbook.com. I am not associated directly with Google, except that I use many of their products. :)
Monday, June 28, 2010
Saturday, June 26, 2010
Import Statements
Importing packages is key to using a lot of the useful functionality available with Go. An import statement can be used with either single line syntax, or with a shortcut multi-line syntax.
Single Line example:
import "fmt"
import "os"
In the shortcut format, this is:
import
(
"fmt"
"os"
)
or:
import(
"fmt"
"os"
)
Notice that you don't have to put the opening parenthesis on the same line as the import keyword. They are not the same as curly braces.
This may not seem like much of a shortcut, but consider importing six or seven libraries (or more). Having to retype import all the time can get a bit tiresome, and is really unnecessary.
Another really cool thing about importing with Go is that the compiler won't let you import a package that you aren't using.
Consider the following:
package main
import
(
"os"
"fmt"
)
func main(){
fmt.Printf("How's it going there?");
}
This will generate an error and you can't compile the program. Why? Because you imported the os package and didn't use it.
Single Line example:
import "fmt"
import "os"
In the shortcut format, this is:
import
(
"fmt"
"os"
)
or:
import(
"fmt"
"os"
)
Notice that you don't have to put the opening parenthesis on the same line as the import keyword. They are not the same as curly braces.
This may not seem like much of a shortcut, but consider importing six or seven libraries (or more). Having to retype import all the time can get a bit tiresome, and is really unnecessary.
Another really cool thing about importing with Go is that the compiler won't let you import a package that you aren't using.
Consider the following:
package main
import
(
"os"
"fmt"
)
func main(){
fmt.Printf("How's it going there?");
}
This will generate an error and you can't compile the program. Why? Because you imported the os package and didn't use it.
Friday, June 25, 2010
Short Declarations
The book has just started to sell, and I am going to point out one thing that some Go purists may find frustrating: I don't use short declarations extensively in the book. They may think that this somewhat violates the whole purpose of the language : to be able to do things quickly.
However, I don't believe this is the case. The book does clarify that you can use short declarations, but I prefer to show how to do things the long way because that is the one that the beginners may find most difficult. The syntax is somewhat "backwards" of what they may expect.
Let's say I want to declare an integer and assign a value to it. If I use short declarative form, this is:
a := 5;
or even shorter
a:= 5
(no semicolon).
This is all fine and dandy, but is not very self-documenting of the variable a. One could argue that simply using a more useful name like numberOfCows or whatever describes the variable's purpose is the solution. I agree to an extent. This is most helpful, especially with primitive data types. But this can get remarkably confusing when the data types are not as clear cut, like various similar custom data types.
Regardless, for the book I chose to show the long declarations because these are self-documenting and if the reader doesn't like long declarations, they can feel free to create their program in short declarative form. There's no harm in declaring an integer variable (or any variable of any other data type) this way:
var a int;
a = 5;
So, pick your poison! Choose whichever feels best for you. The writability of Go is improved by using short declarations, but not necessarily the overall readability, in my opinion.
However, I don't believe this is the case. The book does clarify that you can use short declarations, but I prefer to show how to do things the long way because that is the one that the beginners may find most difficult. The syntax is somewhat "backwards" of what they may expect.
Let's say I want to declare an integer and assign a value to it. If I use short declarative form, this is:
a := 5;
or even shorter
a:= 5
(no semicolon).
This is all fine and dandy, but is not very self-documenting of the variable a. One could argue that simply using a more useful name like numberOfCows or whatever describes the variable's purpose is the solution. I agree to an extent. This is most helpful, especially with primitive data types. But this can get remarkably confusing when the data types are not as clear cut, like various similar custom data types.
Regardless, for the book I chose to show the long declarations because these are self-documenting and if the reader doesn't like long declarations, they can feel free to create their program in short declarative form. There's no harm in declaring an integer variable (or any variable of any other data type) this way:
var a int;
a = 5;
So, pick your poison! Choose whichever feels best for you. The writability of Go is improved by using short declarations, but not necessarily the overall readability, in my opinion.
Where I am on the web
Well, the Go Programming book now has its own Twitter page:
http://twitter.com/goprogbook
and its own Facebook page:
Go Programming Book by J.P. Baugh
Come follow!
http://twitter.com/goprogbook
and its own Facebook page:
Go Programming Book by J.P. Baugh
Come follow!
Thursday, June 24, 2010
Programming Go If You Have Windows
It's a frequently asked question. What do you do if you have a Windows machine? After all, at this point in time, the only fully supported ports of Go are for Mac OS and Linux.
Well, there is an active community involved on porting it to Windows under MinGW.
Check it out HERE .
Another alternative, since the MinGW port isn't completed yet would be to install Ubuntu (one of many flavors of Linux) on your Windows box. This sounds daunting. You don't even have to go through partitioning or anything complicated or potentially dangerous to your file system. You can use a handy-dandy little tool called the Windows Ubuntu Installer, or Wubi for short.
Get Wubi HERE.
Once you have Ubuntu installed using Wubi, it will allow you to choose Ubuntu on your Windows box at startup. Don't worry. If you choose this option, Windows isn't really in any danger, but the Ubuntu installation can be a bit fragile and sensitive to things like hard reboots. But it's a sinch to reinstall if you have something goofy go on. It also uninstalls easily.
Well, there is an active community involved on porting it to Windows under MinGW.
Check it out HERE .
Another alternative, since the MinGW port isn't completed yet would be to install Ubuntu (one of many flavors of Linux) on your Windows box. This sounds daunting. You don't even have to go through partitioning or anything complicated or potentially dangerous to your file system. You can use a handy-dandy little tool called the Windows Ubuntu Installer, or Wubi for short.
Get Wubi HERE.
Once you have Ubuntu installed using Wubi, it will allow you to choose Ubuntu on your Windows box at startup. Don't worry. If you choose this option, Windows isn't really in any danger, but the Ubuntu installation can be a bit fragile and sensitive to things like hard reboots. But it's a sinch to reinstall if you have something goofy go on. It also uninstalls easily.
Curly Brace { } Mayhem
Programmers and enthsiasts new to Go may find the curly brace syntax requirements to be a bit unusual. Misplacing the opening curly brace in functions and other programming structures is a very common mistake.
Let's examine this a little closer:
func main()
{
}
will generate an error, because for programming elements like functions, Go inserts a semicolon after the closing parenthesis. Thus, this is what the compiler actually sees:
func main();
{
}
which is obviously a syntax error.
To prevent this, make sure that you put the opening curly brace on the same line as the function header, as follows:
func main(){
}
With the above syntax template, the program will compile with no problem!
Let's examine this a little closer:
func main()
{
}
will generate an error, because for programming elements like functions, Go inserts a semicolon after the closing parenthesis. Thus, this is what the compiler actually sees:
func main();
{
}
which is obviously a syntax error.
To prevent this, make sure that you put the opening curly brace on the same line as the function header, as follows:
func main(){
}
With the above syntax template, the program will compile with no problem!
Welcome to my Go Programming Blog!
Well, I recently published my book, Go Programming and it is currently available on Amazon, and should be available soon at your local book store. I hope that it helps many people get up and running with this excellent language. I will be posting some information about my experience(s) with the language here, as well as (hopefully) helpful tutorials and information.
Subscribe to:
Posts (Atom)