haruprojectの日記(技術モノ)

日々の技術的な取り組みアウトプット用

visual studio codeのインストール(Ruby on Rails 開発甩)

rails の勉強してます。

そろそろIDEが欲しかったので、探した所RubyMineがよさげだったのですが有料のため代替を探したところVisual Studio Codeというのがあったので使うこととした。

 

Visual Studio Code - Microsoft

Mac, Windows, Linux の軽量/高速な高機能開発エディター. 

Microsoft製品ですが無料。

 

Visual Studio Code - Visual Studio

 

Mac版インストールして起動。以上

code.visualstudio.com

go言語でシンプルなwebサーバーとテンプレートを使うところまで

続き。

フォルダ構成

chat
  templates
      chat.html
  main.go

main.go

package main

import (
	"net/http"
	"log"
	"sync"
	"html/template"
	"path/filepath"
)

type templateHandler struct {
	once     sync.Once
	filename string
	templ    *template.Template
}

// ServeHTTPはHTTPリクエストを処理
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t.once.Do(func() {
		t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
		t.templ.Execute(w, nil)
	})

}

func main() {
        // htmlベタ書きするとき
	//http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	//	w.Write([]byte("htmlコード"))
	//})

	http.Handle("/",&templateHandler{filename:"chat.html"})

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal("ListenAndServe", err)
	}

}


chat.html

<html>
<body>
    チャットしましょう!(テンプレートより)
</body>
</html>

起動して
http://localhost:8080 で確認。

おわり。

haruproject.hateblo.jp
haruproject.hateblo.jp
haruproject.hateblo.jp

go言語の開発環境をIntelliJ IDEA 15でつくる

続き

IntelliJ IDEAをダウンロードしにいく
www.jetbrains.com

Ultimateをダウンロード※ライセンスがあるので
f:id:haruproject:20160216221706p:plain

いつものように配置
f:id:haruproject:20160216221917p:plain

GO SDKのインストールは以下の手順
f:id:haruproject:20160216225314p:plain

f:id:haruproject:20160216225317p:plain

f:id:haruproject:20160216225320p:plain

f:id:haruproject:20160216225324p:plain
インストールが終わったらIDEAを再起動する。

動作確認。サンプルコードをこんな感じで用意

main.go

package main

import "fmt"

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

こんな感じで実行
f:id:haruproject:20160216223330p:plain

コンソールに出力される
f:id:haruproject:20160216223346p:plain

おわり

haruproject.hateblo.jp

haruproject.hateblo.jp

haruproject.hateblo.jp

go言語でnet/httpでWEBサーバをたてる

続き。

routetest.go

package main

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

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func main() {
	http.HandleFunc("/", hello)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
$ go run routetest.go

http://localhost:9090/helloでアクセス

f:id:haruproject:20160215210754p:plain

終わり。

haruproject.hateblo.jp
haruproject.hateblo.jp

go言語でhello worldを"go fmt"してみる

続き。

before

hello.go

package main

import "fmt"

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

hello.goがあるフォルダで。

$ go fmt

after
hello.go

package main

import "fmt" 

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

コードが整形される。以上。

haruproject.hateblo.jp