Montag, 30. November 2009

misunderstanding on casting in go

huch, have needed one day to understand that a function is in really was a cast.
look:
path.Walk(p.String(), pathIter(c), nil);

you can think pathIter(c) is a function. but:

type pathIter chan<- Path

pathIter is a typedefinition. Therefore is pathIter(c) not a function with parameter c, rahter a casting from c (is a Path channel) to pathIter.

Sonntag, 29. November 2009

input from stdion in go


package main

import (
"os"; "fmt"; "bufio";
)

func main() {

fmt.Print("input your name: ");

br := bufio.NewReader(os.Stdin);

name, _ := br.ReadString('\n');

fmt.Println();
fmt.Println("your name is: ", name);

}



func NewReader(rd io.Reader) *Reader NewReader returns a new Reader whose buffer has the default size.

func (b *Reader) ReadString(delim byte) (line string, err os.Error) read until delim byte.
here we read until enter.

Samstag, 28. November 2009

urlescap in http


url := "http://blub.org/b.html?v1=23&v2=bla&v=sdl+sdo";
surl := http.URLEscape(url);

fmt.Println(url);
fmt.Println(surl);

url2 := "http://это.порусс.ки/здесь.хтмл";
surl2 := http.URLEscape(url2);

fmt.Println(url2);
fmt.Println(surl2);


output:
http://blub.org/b.html?v1=23&v2=bla&v=sdl+sdo
http://blub.org/b.html%3fv1%3d23%26v2%3dbla%26v%3dsdl%2bsdo

http://это.порусс.ки/здесь.хтмл
http://%d1%8d%d1%82%d0%be.%d0%bf%d0%be%d1%80%d1%83%d1%81%d1%81.%d0%ba%d0%b8/%d0%b7%d0%b4%d0%b5%d1%81%d1%8c.%d1%85%d1%82%d0%bc%d0%bb


http.URLUnescape(url string) do it back.

read a file and print the count of lines


package main

import (
"io";
"fmt";
"os";
"strings";
)

func main() {
// read the file in bytes array
b, err := io.ReadFile("readfile.go");
if err!=nil {
fmt.Println("error on read file");
os.Exit(-1);
}

// convert from bytes to string
strbuffer := string(b);

// split into lines
lines := strings.Split(strbuffer, "\n", 0);

// iterate each
for i, line := range lines {
fmt.Printf("%d: %s\n", i, line);
}
}

Freitag, 27. November 2009

strings package in golang


package main

import (
"strings";
"fmt";
)

func main() {
data := "bla blu blub";
fmt.Println(data);
// return: bla blu blub

// convert string in bytes
b := strings.Bytes(data);
fmt.Println(b); // print every byte as array
// return: [98 108 97 32 98 108 117 32 98 108 117 98]


// test if the string begin with the prefix
if strings.HasPrefix(data, "bl") {
fmt.Println("data: \"", data, "\" begin with the prefix \"bl\"");
}

// test if the string end with the suffix
if strings.HasSuffix(data, "blub") {
fmt.Println("data: \"", data, "\" end with \"blub\"");
}

// Index returns the index of the first instance of sep in s,
// or -1 if sep is not present in s.
fmt.Println("index: ", strings.Index(data, "blu"));
// return: index: 4

fmt.Println("index: ", strings.Index(data, "b"));
// return: index: 0
fmt.Println("last index: ", strings.LastIndex(data, "b"));
// return: last index: 11
// LastIndex return the last occurency of seeking string

// with Repeat you can repeat ;)
fmt.Println("r: ", strings.Repeat("x", 10));
// return: r: xxxxxxxxxx

// split the string in array of string
as := strings.Split("root:alsdfjl:230:/blub/blub:lalalala", ":", 0);

fmt.Println(as);
// return: [root alsdfjl 230 /blub/blub lalalala]

fmt.Println(strings.Join(as, "|"));
// return: root|alsdfjl|230|/blub/blub|lalalala

fmt.Println(strings.ToLower("UASFalsdfuADSFWE$$"));
// return: uasfalsdfuadsfwe$$

fmt.Println(strings.ToUpper("owenadsfoadsfj"));
// return: OWENADSFOADSFJ

fmt.Println("<", strings.TrimSpace(" asdf sdowe "), ">");
// return: < asdf sdowe >
}

base64 in golang


package main

import (
"encoding/base64";
"bytes";
"fmt";
)

func main() {
// create the buffer
bb := &bytes.Buffer{};
bbURL := &bytes.Buffer{};

// create two base64 encoder (standard and for url)
encoder := base64.NewEncoder(base64.StdEncoding, bb);
encoderURL := base64.NewEncoder(base64.URLEncoding, bbURL);

data := "hallo this is a test, a=23&var2=blabla+-?!%$/\\";
fmt.Println("data : ", data);

// to encode data, use Write([]bytes),
// therefore you must convert string to bytes
// with string.Bytes(d string)
encoder.Write([]byte(data));
encoder.Close();

encoderURL.Write([]byte(data));
encoderURL.Close();

// voila
fmt.Println("encoded Std: ", bb.String());
fmt.Println("encoded URL: ", bbURL.String());
}

Mittwoch, 25. November 2009

time in go


package main

import "fmt"
import "time"

func main() {

fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());

fmt.Println("sleep 1 second.");

time.Sleep(1*1000*1000*1000);

fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());

fmt.Println("wait sleep 1 second.");
before := time.Nanoseconds();
time.Sleep(1*1000*1000*1000);
after := time.Nanoseconds();
fmt.Println("difference of nanoseconds: ", (after-before));

t:=time.LocalTime();
fmt.Println("time.String(): ", t.String());
}


time.Seconds() get the time as unix time in seconds
time.Nanoseconds() dito
time.Sleep(x) sleep for x nanoseconds
to sleep 1 second do time.Sleep(1e9)


to get any type back from function
let look:

package main

import "fmt"
import "rand"
import "time"

func my() interface{} {

rand.Seed(time.Nanoseconds()/1000000);

switch rand.Intn(3) {
case 0: var i int; i=4; return i;
case 1: var f float; f=2.3; return f;
case 2: var s string; s="blub"; return s;
}
var b byte;
b='c';
return b;
}

func main() {
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
}


with func my() interface{} {...} we declare a function which return an interface. This is a void interface, which can implement all types. Thus we can back every typ back. The my() function simple init the rand and get randomly different types back.