天天好心情 (4)

一周一记 本周最囧 没有之一

周六晚上 饱餐之后回家 打开铁门 正要推开里门的时候 发现居然被人锁上了 这扇20年不曾被锁住的门 就这样让我有家不能回了 当时可真是囧啊 又冷又内急 手机还没电 还碰到一个不男不女变性过程中精神也不怎么正常的人和我唠叨 可真把我恶心坏了……

故事的结局也还是好的 我买了个充电器 又住到了朋友家 第二天早上还找房东开了门..

Anyway,几件开心的事吧 –
1. 已经有连着6周去gym了,虽然到现在system里还没有我的名字。这周开始有开始室外跑了,真的是累啊。同时早睡早起也有了点眉目,早上能有一个小时做点私事,可不算坏。

2. 我居然可以穿得上S号的衣服 – 太神奇了!

3. 开始玩Fruit Ninja,并在周五回家之前,拿了个高分 – 至少让R和A同学可以在周末苦干一下吧 😛

4. 时隔小半年,又开始接触C Code的感觉很奇特 – 不是很习惯不自带lib的感觉。

5. 越来越喜欢去各种博物馆,艺术馆尤甚。很不明白的是,为什么早年各种抵触呢?

6. The Social Network也太火了吧..

/^1?$|^(11+?)\1+$/

the above regular expression could be used to check a number is prime or not! is it amazing? like a magic!

but we know there is no magic in CS.. well, this link has a great explanation.

amazing!

Google GO (5) – "Effective Go" Notes

Effective GO link

“gofmt” could do the code formatting for you.

Underscore (_), is the blank identifier, provides a way to ignore values returned by a multi-valued expression.

x, _ = f() // ignore second value returned by f()

use range to help for loop

for _, value := range m { // key is unused
sum += value
}

In switch, cases can be presented in comma-seperated lists

switch c {
case ‘ ‘, ‘?’, ‘&’, ‘=’, ‘#’, ‘+’, ‘%’:
return true
}

Switch can be used to discover the dynamic type of an interface variable.

switch t := interfaceValue.(type) {
default:
fmt.Printf(“unexpected type %T”, type); // %T prints type
case bool:
fmt.Printf(“boolean %t\n”, t);
case int:
fmt.Printf(“integer %d\n”, t);
case *bool:
fmt.Printf(“pointer to boolean %t\n”, *t);
case *int:
fmt.Printf(“pointer to integer %d\n”, *t);
}

Functions support multiple returns, such as

func nextInt(b []byte, i int) (int, int)

Named return parameter is initialized by zero, and return by current value if return with no arguments.

Composite literal, could use

return &File{fd, name, nil, 0};

or labels inside

return &File{fd: fd, name: name}

new() returns the pointer with all zeroed memory.

make(T, args) creates slices, maps and channels.

Arrays, when it is passed to a function, it is a copy of the array, not just the pointer.

The size of an array is part of its type, which means [10]int and [20]int are distinct.

len(slice) -> length, cap(slice) -> capacity

In if statement, the expression may be preceded by a simple statement, which executes before the expression is evaluated.

if x := f(); x < y {

Go’s memory sharing design:

Do not communicate by sharing memory; instead, share memory by communicating.

Buffered channel & Unbuffered channel

ci := make(chan int); // unbuffered channel of integers
cj := make(chan int, 0); // unbuffered channel of integers
cs := make(chan *os.File, 100); // buffered channel of pointers to Files

Channels could be used as semaphore

var sem = make(chan int, MaxOutstanding)

func handle(r *Request) {
sem <- 1; // Wait for active queue to drain.
process(r); // May take a long time.
<-sem; // Done; enable next request to run.
}

Google GO (4) – Tutorial Notes

Tutorial Notes

Three ways of declaration:

var s string = “”;
var s = “”;
s := “”;

For Loop(parentheses no needed, braces mandatory):

for i := 0; i < flag.NArg(); i++ {

To signal an erroneous return, call

os.Exit(1)

Falling off main.main means “success”

Types are different even they are using the same bits. int and int32 are distinct, int and uint are distinct.

Strings are kinda const strings in C++, couldnt be changed.

Arrays

var arrayOfInt [10]int;

Slice variable reference a segment of an array

a[low : high]

function func sum(a []int) int {} could be called in multiple ways

s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum
s := sum(&[…]int{1,2,3}); // want compiler to count the elements
s := sum([]int{1,2,3});

Maps

m := map[string]int{“one”:1 , “two”:2}

Allocation

type T struct { a, b int }
var t *T = new(T);

or

t := new(T);

For maps, slices and channels which have reference semantics, using make() instead

m := make(map[string]int);

If a name (of a top-level type, function, method, constant or variable, or of a structure field or method) is capitalized, it is visible to the public, users of the package may see it. Otherwise, the name and hence the thing being named is visible only inside the package in which it is declared.

Open files

func Open(name string, mode int, perm int) (file *File, err os.Error)

Note there is a multi-value return here. err is nil if no error. Similarly, there are other methods for I/O

func (file *File) Close() os.Error
func (file *File) Read(b []byte) (ret int, err os.Error)
func (file *File) Write(b []byte) (ret int, err os.Error)
func (file *File) String() string

os.Error has a method called os.Error.String() could convert to a printing description.

Switch

switch nr, er := f.Read(&buf); true {
case nr < 0:

case nr == 0:

case nr > 0:

}

Interface is declared like

26 type reader interface {
27 Read(b []byte) (ret int, err os.Error);
28 String() string;
29 }

It is implemented by a type if the type implement *all* the methods declared in the interface. This means following empty interface is by default implemented by any type.

type Empty interface {}

Printf(), %v will print in a simple appropirate style by default. Print() and Println() would do the print fromatting automatically.

Type assertion, e.g. v.(Stringer) while Stringer is an interface, to see if v satisfies the Stringer interface.

s, ok := v.(Stringer); // Test whether v implements “String()”

Channel, a communications channel that connect two concurrent computations, using make() to create new channel.

09 // Send the sequence 2, 3, 4, … to channel ‘ch’.
10 func generate(ch chan int) {
11 for i := 2; ; i++ {
12 ch <- i // Send ‘i’ to channel ‘ch’.
13 }
14 }
15
16 // Copy the values from channel ‘in’ to channel ‘out’,
17 // removing those divisible by ‘prime’.
18 func filter(in, out chan int, prime int) {
19 for {
20 i := <-in; // Receive value of new variable ‘i’ from ‘in’.
21 if i % prime != 0 {
22 out <- i // Send ‘i’ to channel ‘out’.
23 }
24 }
25 }

Go routines, starting the function running in parallel in the same address space

go sum(hugeArray); // calculate sum in the background

Then check the result by passing channel

ch := make(chan int);
go sum(hugeArray, ch);
// … do something else for a while
result := <-ch; // wait for, and retrieve, result

select statement choose which of the multiple communications listed can proceed

21 func server(op binOp, service chan *request, quit chan bool) {
22 for {
23 select {
24 case req := <-service:
25 go run(op, req); // don’t wait for it
26 case <-quit:
27 return;
28 }
29 }
30 }

Google GO (3)

原来还有个Tech Talk (pdf)。果然困了,半点都没看下去 o_O

不过如果GO如传说中编译的那么快的话,yy一下一分钟编译完firefox/OOo(当然要他们改用GO了)的场景,这样我一定没有借口不用Gentoo了。哈哈

letssseeee