How To Define Constants in Google Go

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

In the previous tutorial, we saw how to define variables in Google Go. In today’s tutorial, we check out how we can declare constants and enumerated constants in Google Go.

Constants
The types of constants available in Go programming are boolean constants, integer constants, floating-point constants, complex constants, and string constants. Constants in Go are created at compile time, even when defined as locals in functions.

Constants are declared similar to variables except that the const keyword is used. Also we cannot use the idiom (using :=) as done for variable declaration.

Below are examples of declaring different types of constants in Go programming.

const Pi float64 = 3.14159265358979323846   //typed floating-point constant
const zero = 0.0             // untyped floating-point constant
const (
        size int64 = 1024   //typed integer constant
        eof = -1             // untyped integer constant
)
const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float = 0, 3      // u = 0.0, v = 3.0
const sum = 1 – 0.707i   ///complex constant
const flag bool = true

Enumerated Constants
In Go, enumerated constants are created using the iota enumerator. This can be considered similar to enumeration in C. It is reset to 0 whenever the reserved word const appears in the source and increments after each use of iota as shown below.

const (  // iota is reset to 0
        c0 = iota  // c0 == 0
        c1 = iota  // c1 == 1
        c2 = iota  // c2 == 2
)

const (
        a = 1 << iota  // a == 1 (iota has been reset)
        b = 1 << iota  // b == 2
        c = 1 << iota  // c == 4
)

const (
        u       = iota * 42  // u == 0     (untyped integer constant)
        v float = iota * 42  // v == 42.0  (float constant)
        w       = iota * 42  // w == 84    (untyped integer constant)
)

const x = iota  // x == 0 (iota has been reset)
const y = iota  // y == 0 (iota has been reset)
 

Related Posts:

Tags:

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: