2012/09/08

メモ

MixedCaps 最後になりますが、Go言語の慣例では、複数の単語から成る名前をつけるときはアンダースコアを使わずに、MixedCapsまたはmixedCapsのように単語の先頭だけ大文字を用います。
for pos, char := range "日本語" {
    fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
次が出力されます。 character 日 starts at byte position 0 character 本 starts at byte position 3 character 語 starts at byte position 6
string を []byte に変換する []byte(s)
型アサーション 次の形式はインタフェース型の値xをT型に型アサーションする基本式です x.(T) この式はxはnilではなく、かつxにはT型の値が格納されていると断定します。このx.(T)という表記は、型アサーションと呼ばれます。
switchは、インタフェース変数の動的な型を見つけるために用いることもできます。その型switchには、型アサーションの構文を使って丸括弧()の中にキーワード"type"と書きます。switchの式で変数を宣言したとき、その変数は各case節において適切な型となります。
switch t := interfaceValue.(type) {
default:
    fmt.Printf("unexpected type %T", t)  // %T は型を出力する
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)
}
package main

import (
    "log";
    "time";
)

func FibIter(n int) chan uint64 {
    ch := make(chan uint64);
    go func() {
        var a, b uint64 = 0, 1;
        for i := 0; i < n; i++ {
            ch <- a;
            a, b = b, a + b;
        }
        close(ch);
    }();
    return ch;
}

func main() {
    for i := range FibIter(50) {
        log.Stdout(i);
        time.Sleep(1e8);
    }
}
// 式の解析に失敗した時に返されるエラーコード
var (
    ErrInternal      = os.NewError("regexp: internal error")
    ErrUnmatchedLpar = os.NewError("regexp: unmatched '('")
    ErrUnmatchedRpar = os.NewError("regexp: unmatched ')'")
    ...
)
m := map[string]string{}
m["abc"] = "ABC"

v, ok := m["abc"]
fmt.Printf("'%s' %t\n", v, ok)

v, ok = m["xyz"]
fmt.Printf("'%s' %t\n", v, ok)
'ABC' true
'' false
package main

import (
 "unsafe"
 "fmt"
)

type TypeA struct {
 x string
}

type TypeB struct{
 TypeA
 m map[string]string
}

type TypeC interface {
 Get() int
 Set(value int)
}

type ImplC struct {
}

func (c *ImplC) Get() int {
 return 0
}

func (c *ImplC) Set(value int) {
}

func main() {
 a := TypeA{}
 fmt.Println("case 1: sizeof(TypeA)", unsafe.Sizeof(a))

 a.x = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
 fmt.Println("case 2: sizeof(TypeA)", unsafe.Sizeof(a))

 b := TypeB{TypeA{}, make(map[string]string)}
 fmt.Println("case 3: sizeof(TypeB)", unsafe.Sizeof(b))

 var i interface{}
 i = &a
 fmt.Println("case 4: sizeof(interface{})", unsafe.Sizeof(i))
 
 i = b
 fmt.Println("case 5: sizeof(interface{})", unsafe.Sizeof(i))

 var c TypeC
 c = &ImplC{}

 fmt.Println("case 6: sizeof(TypeC)", unsafe.Sizeof(c))
}
case 1: sizeof(TypeA) 16
case 2: sizeof(TypeA) 16
case 3: sizeof(TypeB) 24
case 4: sizeof(interface{}) 16
case 5: sizeof(interface{}) 16
case 6: sizeof(TypeC) 16
package main

import (
 "fmt"
)

func main() {
 var x []interface{}
 x = append(x, "abc")
 x = append(x, 123)

 for n, v := range x {
  fmt.Printf("[%d] = '%v'\n", n, v)
 }
}
func begin() int {
 fmt.Println("begin()")
 return 0
}
func inc(value int) int {
 fmt.Println("inc()", value, "=>", value + 1)
 return value + 1
} 

func cond(value int) bool {
 fmt.Println("cond()", value)
 return value < 3
}

func main() {
 for n := begin(); cond(n); n = inc(n) {
  fmt.Println("in loop.")
 }
}

begin()
cond() 0
in loop.
inc() 0 => 1
cond() 1
in loop.
inc() 1 => 2
cond() 2
in loop.
inc() 2 => 3
cond() 3
start := time.Now();
// 処理
end := time.Now();
fmt.Printf("%f秒\n",(end.Sub(start)).Seconds())
end.Sub(start) は、end - start
Seconds()関数は、float64を返します。

0 件のコメント:

コメントを投稿