Last active
May 25, 2018 14:58
-
-
Save sekky0905/c182dc0623b72f0c7ab18344a64aaa5b to your computer and use it in GitHub Desktop.
GoのTemplateのPipelines(パイプライン)を理解しながら、テンプレート内で関数にスライスを引数で渡してみる ref: https://qiita.com/Sekky0905/items/fdbbad75d52e8336a6aa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"html/template" | |
"net/http" | |
"log" | |
) | |
// メイン関数 | |
func main() { | |
// ハンドラをバンドル | |
http.HandleFunc("/", Handler) | |
// リスナー | |
if err := http.ListenAndServe(":8080", nil); err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} | |
// テンプレートを生成 | |
func Handler(w http.ResponseWriter, r *http.Request) { | |
// 構造体Programを生成 | |
p := &Program{Languages:[]string{"Go", "Java", "Python", "JavaScript"},} | |
// 関数を名前をつけてtemplateのFuncMapに登録 | |
funcMap := template.FuncMap{ | |
"judgeLangType" : JudgeLangType, | |
} | |
// テンプレート部分 | |
text := ` | |
Go : {{ index .Languages 0 | judgeLangType}} | |
Java : {{ index .Languages 1 | judgeLangType}} | |
Python : {{ index .Languages 2 | judgeLangType}} | |
JavaScript : {{ index .Languages 3 | judgeLangType}} | |
` | |
// テンプレートをパースして、http.ResponseWriterに書き込み | |
tmpl := template.Must(template.New("calculator").Funcs(funcMap).Parse(text)) | |
tmpl.Execute(w, p) | |
} | |
// 構造体 | |
type Program struct { | |
Languages []string | |
} | |
// 言語を引数に受けて、その言語の型を返す | |
func JudgeLangType(lang string) string{ | |
switch lang { | |
case "Go", "Java": | |
return "Static" | |
case "Python", "JavaScript": | |
return "Dynamic" | |
default: | |
return "unknown" | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Go : Static | |
Java : Static | |
Python : Dynamic | |
JavaScript : Dynamic | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// テンプレート部分 | |
text := ` | |
Go : {{ index .Languages 0 | judgeLangType}} | |
Java : {{ index .Languages 1 | judgeLangType}} | |
Python : {{ index .Languages 2 | judgeLangType}} | |
JavaScript : {{ index .Languages 3 | judgeLangType}} | |
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Go : {{ index .Languages 0 | judgeLangType}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment