Personal tools
You are here: Home ブログ uchida Categories Go
« December 2010 »
Su Mo Tu We Th Fr Sa
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Categories
JavaScript
Go
Ada
Delphi
junk
 
Document Actions

Go

Up one level

Document Actions

UbuntuにGoをインストールしてみた

アリエルでは最近「最新技術に飛びついて注目されようぜ」というムードがあります。
私としてはもっと古い言語を覚えてみたいんですけどね。
AdaとかLEGOとかForthとかIoとかSmalltalkとかAdaとか。

とはいえ知らない言語を使ってみるのは嫌いじゃないんで、今アリエルで最も熱い言語「Go」をさわってみました。
基本はここにある通りですが、いろいろはまったんで書いておきます。
http://golang.org/doc/install.html
大体この人と同じところで躓きました。なのでほとんどこちらの方のサイトのコピペになってしまいました。
http://d.hatena.ne.jp/kidd-number5/20091111/1257939793


まずMercurialのインストール必要がありますが、Synapticからインストールすると少し古いバージョンのものがインストールされます。
こいつはプロキシ経由でhttpsにアクセスできないバグがあるのでインストールしてはいけません。
アリエルでは外部のネットワークに接続するためにプロキシを使用しているため、ここで苦戦しました。
http://d.hatena.ne.jp/ursm/20080820/1219250897

最新版をインストールするにはソースからビルドするかeasy_installでインストールします。
easy_installをインストールするにはez_setup.pyをダウンロードします。
http://labs.unoh.net/2007/04/python.html

Mercurialのビルドにはpython-develが必要です。
Synapticからpython-all-devをインストールします。

プロキシの設定は~/.hgrcにこんな感じ。

[http_proxy]
host=123.45.67.89:1234

あとPATHを設定するので~/.bash_profileに以下を追加して、

export GOROOT=$HOME/go
export GOOS=linux
export GOARCH=386
export GOBIN=$HOME/go/bin
PATH=$PATH:$GOBIN

読み込ませます。

$ source ~/.bash_profile

全部できたらやっとGoのソースを取得できます。

$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

取得できたらディレクトリの作成

$ mkdir $GOBIN

あとbisonがないとビルドに失敗するのでインストールします。
ビルド

$ ./all.bash

最後に$GOROOT/misc/emacs/以下にあるelファイルをemacsのパスの通ったところにおいてバイトコンパイルします。
そして.emacsファイルに以下を追記します。
(何言ってんのかわかんねえ、って人はこの辺無視してください。アリエルにはemacsって書くと喜ぶ人が多いんです)

(require 'go-mode-load)
(add-hook 'go-mode-hook
'(lambda()
(setq tab-width 4)

GoのトップページにあるHello Worldをコピペしてこんにちは。

$ ~/go/bin/8g hello.go
$ ~/go/bin/8l hello.8
$ ./8.out
Hello, 世界

試しにGo目並べを作ってみました。
「Go目並べ」言いたかっただけです。
gomoku.zip

package main

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

const BOARD_LENGTH = 20

const PLAYER1 = "*";
const PLAYER2 = "@";

type Board [BOARD_LENGTH*BOARD_LENGTH]int

func (b *Board) Display() {
fmt.Print(" ");
for i := 0; i < BOARD_LENGTH; i++ {
fmt.Printf("%3d", i);
}
fmt.Println();

for i, n := range *b {
x := i % BOARD_LENGTH;
if x == 0 {
fmt.Printf("%3d", i / BOARD_LENGTH);
}

switch n {
case 1: fmt.Printf(" %s", PLAYER1);
case 2: fmt.Printf(" %s", PLAYER2);
default: fmt.Print(" -");
}
if x == BOARD_LENGTH - 1 {
fmt.Println();
}
}
}

func inRange(n int) bool {
return 0 <= n && n < BOARD_LENGTH;
}

func (b *Board) Put(x, y, n int) bool {
if (!inRange(x) || !inRange(y)) {
fmt.Println("out of index");
return false;
}
index := x + y * BOARD_LENGTH;
if b[index] != 0 {
fmt.Println("already put stone");
return false;
}
b[index] = n;
return true;
}

func (b *Board) IsWinner(putx, puty, putn int) bool {
for i := 0; i < 4; i++ {
var xx, yy int;
switch i {
case 0: xx, yy = -1, -1;
case 1: xx, yy = -1, 0;
case 2: xx, yy = 0, -1;
case 3: xx, yy = -1, 1;
}

x, y := putx, puty;
five := 1;
d := 1;
for {
x += (xx * d);
y += (yy * d);
if inRange(x) && inRange(y) &&
b[x + y * BOARD_LENGTH] == putn {
if five++; five >= 5 {
return true;
}
continue;
}

if d == -1 {
break;
}
x, y = putx, puty;
d = -1;
}
}
return false;
}

func main() {
var stones = []string{" ", PLAYER1, PLAYER2};
turn := 1;
board := new(Board);
board.Display();


fmt.Printf("player%d's(%s) turn\n", turn, stones[turn]);

reXY := regexp.MustCompile(`^ *([0-9]+) +([0-9]+) *\n$`);

in := bufio.NewReader(os.Stdin);
for {
lineBuf, err := in.ReadString('\n');
if err != nil {
fmt.Fprintln(os.Stderr, "ReadLine err:", err);
os.Exit(2);
}
line := string(lineBuf);
if line == "exit\n" {
os.Exit(0);
}
params := reXY.MatchStrings(line);

if len(params) > 0 {
x, _ := strconv.Atoi(params[1]);
y, _ := strconv.Atoi(params[2]);
if board.Put(x, y, turn) {
board.Display();
if board.IsWinner(x, y, turn) {
fmt.Printf("player%d win\n", turn);
os.Exit(0);
}

if turn == 1 { turn = 2; }
else { turn = 1; }
fmt.Printf("player%d's(%s) turn\n", turn, stones[turn]);
}
}
}
}

さわってみた感想は次回書きます。
唐突ですが私の得意言語はDelphi(Object Pascal)です。

Category(s)
Go
junk
The URL to Trackback this entry is:
http://dev.ariel-networks.com/Members/uchida/ubuntu306bgo309230a430f330b930c830fc30eb30573066307f305f/tbping

Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.