ComputingStuff

all about go, ruby, python, perl, java, scala, osgi, RCP computing, network, hacking, security, encryption. and other interesting stuff

Montag, 7. Dezember 2009

Container in Go: heap, list, ring, vector

List: (a doubly linked list)

The list is a double linked list of Elements. Element is an element in the linked list with Next() and Prev() pointer to other elements. Begining and ending of Elements are nil.


package main

import (
. "fmt";
"container/list";
"strings";
"bytes";
)

// print the elements of the list
func PrintList(l *list.List) {
Println("------------------------------");
for value := range l.Iter() {
Println("element: ", value);
}
Println("------------------------------");
}

func main() {
// create a new list
li := list.New();

// add elements at end
li.PushBack("list1");
li.PushBack("list2");

// add elements at begining
li.PushFront("list3");

PrintList(li);

// get the first element from the list
element := li.Front();
str := element.Value;
Println("value: ", str);

// add a element before the first element
li.InsertBefore("list4", element);

// add after the element
li.InsertAfter("list5", element);

PrintList(li);

// get the next element
element = element.Next();
Println("value: ", element.Value);

// move the first element to back
element = li.Front();
li.MoveToBack(element);

PrintList(li);

// remove the first element
element = li.Front();
li.Remove(element);

PrintList(li);

// find the element with "list2" and remove it
for element := li.Front(); element != nil; element = element.Next() {
// don't use li.Iter(), because the interface chanel
// is geting the Value from the element back. not the element.
value := element.Value.(string);

if bytes.Equal(strings.Bytes(value),strings.Bytes("list2")) {
li.Remove(element);
}
}

PrintList(li);

}


the output is:

------------------------------
element: list3
element: list1
element: list2
------------------------------
value: list3
------------------------------
element: list4
element: list3
element: list5
element: list1
element: list2
------------------------------
value: list5
------------------------------
element: list3
element: list5
element: list1
element: list2
element: list4
------------------------------
------------------------------
element: list5
element: list1
element: list2
element: list4
------------------------------
------------------------------
element: list5
element: list1
element: list4
------------------------------



Ring:

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.

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";
"strings";
"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(strings.Bytes(data));
encoder.Close();

encoderURL.Write(strings.Bytes(data));
encoderURL.Close();

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