How To Define Variables In Google Go

July 28th, 2010 by Kevin | No Comments | Filed in Go Programming

In the previous tutorial, we began by writing a Hello World program in Google Go. This tutorial will look into the available primitive data types and how to declare and define variables in Go programming.

Basic Data Types
The following are the basic data types that are available in the Google Go programming language.

bool          boolean truth values of either true or false
uint8         the set of all unsigned  8-bit integers (0 to 255)
uint16       the set of all unsigned 16-bit integers (0 to 65535)
uint32       the set of all unsigned 32-bit integers (0 to 4294967295)
uint64       the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8          the set of all signed  8-bit integers (-128 to 127)
int16        the set of all signed 16-bit integers (-32768 to 32767)
int32        the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64        the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        familiar alias for uint8

uint         either 32 or 64 bits
int          either 32 or 64 bits
float       either 32 or 64 bits

string      represents the set of string values

To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

Variables

A computer variable can represent any kind of data that can be stored in a computer system.
Variables in the Go programming language can be declared as,

    var s string = ""

This is the var keyword, followed by the name of the variable, followed by its type, followed by an equals sign and an initial value for the variable.
Go tries to be terse, and this declaration could be shortened. Since the string constant is of type string, we don’t have to tell the compiler that. We could write

    var s = ""

or we could go even shorter and write the idiom

    s := ""

Following are several example of declaring different types of variables in Go.

var i int
var U, V, W float
var k = 0
var x, y float = -1, -2
var (
        i int
        u, v, s = 2.0, 3.0, "bar"
)

If no initial value is given to a variable, then that variable is initialized to it’s zero value. False for booleans, 0 for integers, 0.0 for floats, “” for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.

Type Conversions
Go does not support implicit type conversion. To convert a numeric value from one type to another is a conversion, with syntax like a function call:

  uint8(int_var)     // truncate to size
  int(float_var)     // truncate fraction
  float64(int_var) // convert to float

Also some conversions to string:

  string(0x1234)            // == "\u1234"
  string(array_of_bytes)    // bytes -> bytes
  string(array_of_ints)     // ints -> Unicode/UTF-8
 

Related Posts:

Tags:

How To Write A Hello World Program In Google Go

July 26th, 2010 by Kevin | No Comments | Filed in Go Programming

In the previous post, we saw how to install Google’s Go programming language.

Learning any programming language begins with the “Hello World” program. So keeping with the tradition and assuming that you have some programming background, here is the program written in the Google Go programming language.

Open your favorite text editor and type the following program in it.

package main

import "fmt"   //Package implementing formatted I/O

func main(){
   fmt.Printf("hello, world\n")
}

Save the file as “hello.go”.

From the console, compile it using

$ 6g hello.go

To link the file, use

$ 6l hello.6

and to run it

$ ./6.out

This will print,

hello, world

The first line in the program

package main

specifies the name of the package that the file “hello.go” belongs to. The package keyword is used to define the package.

This program imports the package “fmt” to gain access to fmt.Printf. We shall see more about packages in later tutorials.

import "fmt"

Functions are introduced with the func keyword. The main package’s main function is where the program starts running (after any initialization). We shall learn more about functions in later tutorials.

func main()

String constants can contain Unicode characters, encoded in UTF-8. The ‘\n’ is the newline character as in C/C++.

The comment convention is the same as in C++:

/* ... */
// ...

Did you find this tutorial on the Google Go programming language useful? Would you want more of such tutorials for learning about this great new programming language by Google?

 

Related Posts:

Tags:

How To Install Google’s Go Programming Language

July 24th, 2010 by Kevin | No Comments | Filed in Go Programming

Introduction

Go is a compiled, garbage-collected, concurrent programming language developed by Google.

According to the Go website,

  • Go is simple.
  • Go is fast as typical builds take a fraction of a second and resulting programs run nearly as quickly as C or C++ code.
  • Go is type safe and memory safe.
  • Go promotes writing systems and servers as sets of lightweight communicating processes, called goroutines.
  • Go feels like a dynamic language but has the speed and safety of a static language.
  • Go is open source.

Google feels that there is a need for another programming language because:

  • Computers are enormously quicker but software development is not faster.
  • Dependency management is a big part of software development today but the “header files” of languages in the C tradition are antithetical to clean dependency analysis—and fast compilation.
  • There is a growing rebellion against cumbersome type systems like those of Java and C++, pushing people towards dynamically typed languages such as Python and JavaScript.
  • Some fundamental concepts such as garbage collection and parallel computation are not well supported by popular systems languages.
  • The emergence of multicore computers has generated worry and confusion.

Installation

There are two Go compiler implementations, 6g and friends, generically called gc, and gccgo. The 6g (and 8g and 5g) compiler is named in the tradition of the Plan 9 C compilers, described in http://plan9.bell-labs.com/sys/doc/compiler.html6 is the architecture letter for amd64 (or x86-64, if you prefer), while g stands for Go. gccgo is a more traditional compiler using the gcc back end.

Go implementations are currently available for the Linux and Mac OS X platforms. For Windows installation, you can try Go under MS Windows.

This article focuses on installing the gc compiler for Ubuntu 10.04. However the steps should be similar for any Linux distribution.

Installing the gc compiler:

1) Set up the environment variables

Set these variables in your shell profile ($HOME/.bashrc)

export GOROOT=$HOME/go #The root of the Go tree
export GOARCH=amd64 #The name of the target operating system (386, amd64, arm)
export GOOS=linux #The name of the compilation architecture (darwin, freebsd, linux, nacl)
export GOBIN=$HOME/go/bin #The location where binaries will be installed
export PATH=$PATH:$GOBIN

2) Install the C tools

To build Go, you need to have GCC, the standard C libraries, the parser generator Bison, makeawk, and the text editor ed installed. On OS X, they can be installed as part of Xcode.

$ sudo apt-get install bison gcc libc6-dev ed gawk make

3) Fetch the repository

You need Mercurial installed to get the Go repository. On Ubuntu, you can install this as:

$ sudo apt-get install mercurial

Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:

$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

4) Install Go

To build the Go distribution, run

$ cd $GOROOT/src
$ ./all.bash

If all goes well, it will finish by printing

--- cd ../test
N known bugs; 0 unexpected bugs

where N is a number that varies from release to release.

5) Test the installation

A sample program

package main

import "fmt"

func main()
{
   fmt.Printf("hello, world\n")
}

Compile it using

$ 6g hello.go

To link the file, use

$ 6l hello.6

and to run it

$ ./6.out

Now you should be able to compile and run programs written in the Go Programming language.

6) Keeping up with releases

New releases are announced on the Go Nuts mailing list. To update an existing tree to the latest release, you can run:

$ cd $GOROOT/src
$ hg pull
$ hg update release
$ ./all.bash

You can get more details about installing the Go gc compiler here.

Installing the gccgo compiler:

You can get more details about installing the Go gccgo compiler here.

 

Related Posts:

Tags: