LoginSignup
21
22

More than 5 years have passed since last update.

Centos6.5 に Go.lang と mysql ドライバをインストールしてみた

Last updated at Posted at 2015-01-05

OS別の最新版ダウンロード URL

GO言語のダウンロードは以下から行います

https://golang.org/dl/

Centos6.5でのGOインストール手順

ディストリビューションの確認

cat /etc/redhat-release                                                                                       
----
CentOS release 6.5 (Final)

インストール

cd /usr/local/src
wget https://storage.googleapis.com/golang/go1.4.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.4.linux-amd64.tar.gz

パスの設定

export GOPATH=/usr/local/go/
export PATH=$PATH:/usr/local/go/bin

.bashrc にパスを追記

次回ログイン時も有効になるように設定しておく

cat << '_EOT_' >> ~/.bashrc
GOPATH=/usr/local/go/
PATH=$PATH:/usr/local/go/bin
_EOT_

MySQLドライバのインストール

go get github.com/go-sql-driver/mysql

サンプルプログラムの実行

cat << '_EOT_' > /tmp/hello.go

package main

import "fmt"

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


go run /tmp/hello.go
---
hello, world

上記サンプルをコンパイルして実行

go build /tmp/hello.go
./hello 
---
hello, world

httpを書いてみる


cat << '_EOT_' > /tmp/http.go

package main

import (
    "fmt"
    "html"
    "net/http"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
    title := html.EscapeString(req.URL.Path[1:])
    strpost := req.FormValue("strpost")
    strget := req.FormValue("strget")
    output := `
<html>
  <head>
   <title>` + title + `</title>
  </head>
  <body>
  <h1>post/getのテスト</h1>
     <h2>post</h2>
        post=`+ html.EscapeString(strpost) + `</br>
     <h2>get</h2>
        get=`+ html.EscapeString(strget) + `</br>
  </body>
</html>
`
fmt.Fprintf(w, "%s", output)
}

func main() {
    http.HandleFunc("/hello", HelloServer)
    http.ListenAndServe(":8080", nil)
}
_EOT_

go run /tmp/http.go
21
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
22