Samstag, 21. November 2009

learning go

so there will be some golang tips.

type My struct {
a, b int;
name string;

create a struct with three variables, a and b are integers. name is a string. The struct name is My.

m := My{1,2,"ray"};

m:= XXX{inputs} create and initialize the struct to variable m.
Println(m) print out: {1 2 ray}.

to create you own methods for the stuct do: func (v *Mystruct) method(...) returntyps { ... }

func (m *My) String() string {
ret := fmt.Sprintf("a: %d, b: %d, name: %s\n", m.a, m.b,;
return ret;

here we create the String() method for printing our struct as a string. Access variables are done through m.var.

to define a simple array of integers: a:=[3]int{1,2,3}
if you don't know the length: a:=[]int{1,2,3,4,5,1,2,3}

create an assoziative array or dictionary or hash: hash := map[string]int{"eins":1, "zwei":2, "drei":3}; where map[keytyp]valuetyp.

hash2 := map[string]string{"blub":"aodu", "a":"ousdf", "ouew":"gofdu"};

// iterate through the hash, range return the key and value of the hash.
for k, v := range hash2 {
fmt.Println("key: ",k," value: ",v);

print out:
key: blub value: aodu
key: ouew value: gofdu
key: a value: ousdf

Read a directory:

package main

import (

func main() {
dir, err := io.ReadDir("/home/ray/daten");

fmt.Println("err: ", err);

for _, v := range dir {
fmt.Println(">>", v);

let look the source on src/pkg/io/utils.go:

// ReadDir reads the directory named by dirname and returns
// a list of sorted directory entries.
func ReadDir(dirname string) ([]*os.Dir, os.Error) {
f, err := os.Open(dirname, os.O_RDONLY, 0); // open you directory
if err != nil {
return nil, err
list, err := f.Readdir(-1); // f is a file handler
if err != nil {
return nil, err
dirs := make(dirList, len(list));
for i := range list {
dirs[i] = &list[i]
return dirs, nil;

func ReadDir(dirname string) ([]*os.Dir, os.Error) : ReadDir reads the directory named by dirname and returns a list of sorted directory entries.

func Open(name string, flag int, perm int) (file *File, err Error): Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. It returns the File and an Error, if any.

Open return a File pointer. internal Open called a syscall to open

func (file *File) Readdir(count int) (dirs []Dir, err Error): Readdir reads the contents of the directory associated with file and returns an array of up to count Dir structures, as would be returned by Stat, in directory order. Subsequent calls on the same file will yield further Dirs. A negative count means to read until EOF. Readdir returns the array and an Error, if any.

our io.ReadDir() open the dir, read it through the file, create a list, sort and return the dir list.

for _, v := range dir {
if v.IsDirectory() {
fmt.Println(v.Name," is directory");

print out all direcotries name.

more info about dir is in: src/pkg/os/types.go or

Read a file

fileObj, err := os.Open( "echo.go", os.O_RDONLY, 0666 );
if err != nil {
log.Stderr( err );

//open the file and test for errors. log.Stderr print the message with a timestamp as a log.

// create a buffered reader
r := bufio.NewReader( fileObj );

// read each line
for {
line, err := r.ReadString('\n');

Keine Kommentare:

Kommentar veröffentlichen