2009年11月16日月曜日

golang メモメモ

メンバ変数だけの構造体を定義したり、
メンバ関数だけのインタフェースを定義する。

C#3.0の拡張メソッドのようにメンバ関数を実装していく。

特徴的やなーと思ったのが複数の戻り値を定義できること。
戻り値もreturnで値を返すのではなく、関数中に代入して関数の終わりはreturn;というのもあり。

ループは全てForで統一
Cのfor や while 無限ループの3種類を一つのforで扱える。
do-whileは記述できるのかは不明。

elseifでつなげていたのをswitchで書けるのが便利そう。


switch {
case a < b:
  return -1
case a == b:
  return 0
case a > b:
  return 1
}


とりあえずこんな感じで。

まとまりねー。
.

2009年11月14日土曜日

Googleのプログラミング言語 GOのインストール(MacOS X)

Googleのプログラミング言語GOがリリースされたということで、
go lang 公式サイト:http://golang.org/
GOプログラミング言語のサイトにインストール方法が載っていたので、
手元のMacbookにインストールをしてみました。
http://golang.org/doc/install.html

まずは環境変数を設定。


MacBook:~ mezashi184$ vi .bash_profile
export GOROOT=$HOME/go
export GOOS=darwin
export GOARCH=386
export GOBIN=$GOROOT/bin
export PATH=$GOBIN:$PATH


設定した内容を反映。

MacBook:~ mezashi184$ source .bash_profile


ちゃんと反映されているか確認します。

MacBook:~ mezashi184$ env | grep '^GO'
GOBIN=/Users/mezashi184/go/bin
GOARCH=386
GOROOT=/Users/mezashi184/go
GOOS=darwin


hgコマンドがあるか確認。

MacBook:~ mezashi184$ which hg
/usr/local/bin/hg


hgコマンドが無い場合はインストールします。

MacBook:~ mezashi184$ sudo easy_install mercurial
Searching for mercurial
Reading http://pypi.python.org/simple/mercurial/
Reading http://www.selenic.com/mercurial
Best match: mercurial 1.3.1
Downloading http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
Processing mercurial-1.3.1.tar.gz
Running mercurial-1.3.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-JZ0oPU/mercurial-1.3.1/egg-dist-tmp-uLZ_gk
zip_safe flag not set; analyzing archive contents...
mercurial.extensions: module references __file__
mercurial.i18n: module references __file__
mercurial.lsprof: module references __file__
mercurial.templater: module references __file__
No eggs found in /tmp/easy_install-JZ0oPU/mercurial-1.3.1/egg-dist-tmp-uLZ_gk (setup script problem?)


しかし!エラーが出てしまいました。

No eggs found in /tmp/easy_install-JZ0oPU/mercurial-1.3.1/egg-dist-tmp-uLZ_gk (setup script problem?)


エラーを取り除くために以下のサイトを参考にセットアップツールをインストール
http://douglasfshearer.com/blog/easy_install-leopard-bug-no-eggs-found

MacBook:~ mezashi184$sudo easy_install http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg
Downloading http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg
Processing setuptools-0.6c9-py2.5.egg
Moving setuptools-0.6c9-py2.5.egg to /Library/Python/2.5/site-packages
Adding setuptools 0.6c9 to easy-install.pth file
Installing easy_install script to /usr/local/bin
Installing easy_install-2.5 script to /usr/local/bin

Installed /Library/Python/2.5/site-packages/setuptools-0.6c9-py2.5.egg
Processing dependencies for setuptools==0.6c9
Finished processing dependencies for setuptools==0.6c9


再度mercurialをインストール今度は正しくインストールできました♪

MacBook:~ mezashi184$ sudo easy_install mercurial
Searching for mercurial
Reading http://pypi.python.org/simple/mercurial/
Reading http://www.selenic.com/mercurial
Best match: mercurial 1.3.1
Downloading http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
Processing mercurial-1.3.1.tar.gz
Running mercurial-1.3.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-9-mUQT/mercurial-1.3.1/egg-dist-tmp-NfqiQx
zip_safe flag not set; analyzing archive contents...
mercurial.extensions: module references __file__
mercurial.i18n: module references __file__
mercurial.lsprof: module references __file__
mercurial.templater: module references __file__
Adding mercurial 1.3.1 to easy-install.pth file
Installing hg script to /usr/local/bin

Installed /Library/Python/2.5/site-packages/mercurial-1.3.1-py2.5-macosx-10.5-i386.egg
Processing dependencies for mercurial
Finished processing dependencies for mercurial


golangのソースコードを取得します。

MacBook:~ mezashi184$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
requesting all changes
adding changesets
adding manifests
adding file changes
added 4016 changesets with 16888 changes to 2931 files
updating working directory
1640 files updated, 0 files merged, 0 files removed, 0 files unresolved


ソースコードをビルドします。

MacBook:~ mezashi184$ cd go/src
MacBook:~ mezashi184$ mkdir $GOBIN
MacBook:~ mezashi184$ ./all.bash


以下のソースコードをコンパイル・実行してみます。

MacBook:~ mezashi184$ vi hello.go
package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

MacBook:~ mezashi184$ 8g hello.go
MacBook:~ mezashi184$ 8l hello.8
MacBook:~ mezashi184$ ./8.out
Hello, World


これにてインストールと動作確認おしまい。

しかし、インストールしたものの何に使えるのかな?
.

ラベル