MCPcopy Index your code
hub / github.com/chai2010/gotlang

github.com/chai2010/gotlang @v0.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.0.1 ↗ · + Follow
35 symbols 44 edges 4 files 1 documented · 3% updated 1y ago★ 38
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

狗头语言(Go Template Language)

狗头语言是“柴树杉(chai2010)”于2019年11月基于Go语言标准库text/template包定制的图灵完备的玩具语言。

安装

$ go get github.com/chai2010/gotlang/got

例子1:输出命令行参数

创建hello.gotmpl程序,输出命令行参数:

{{/* 狗头语 版权 @2019 柴树杉 */}}}

{{template "main" .}}

{{define "main"}}
    {{range $i, $v := . }}
        {{println $i $v}}
    {{end}}
{{end}}

其中"{{}}"中间的是程序的语句,语句支持传统C语言风格的注释。然后通过{{template "main" .}}执行名字为"main"的模板,执行的参数是.表示的当前对象。这里的main模板可以看作是宏或者是函数,template指令会展开对应的宏函数,因此就产生了类似函数调用的效果(不过这个函数没有返回值)。然后通过range指令执行循环,每次循环打印相关信息。

运行脚本:

$ got hello.gotmpl aa bb cc
0 aa
1 bb
2 cc

执行时第一个参数是程序的路径,后面的参数是传递给狗头语脚本的命令行参数,也就是之前提到的.表示的当前对象。然后就可以输出命令行参数了。

例子2:打印斐波那契数列

创建./examples/fib.gotmpl程序,输出斐波那契数列:

{{/* 狗头语 版权 @2019 柴树杉 */}}}

{{template_call "main" .}}

{{define "main"}}
    {{range $_, $i := (xrange 10) }}
        {{printf "%d: %d\n" $i (template_call "fib" $i)}}
    {{end}}
{{end}}}

{{define "fib"}}
    {{if (le . 0)}}
        {{template_ret 0}}
        {{/*return fib(0)*/}}
    {{else if (eq . 1)}}
        {{template_ret 1}}
        {{/*return fib(1)*/}}
    {{else}}
        {{template_ret (add (template_call "fib" (sub . 1)) (template_call "fib" (sub . 2)))}}
        {{/*return fib(n-1) + fib(n-2)*/}}
    {{end}}
{{end}}

这个例子更为复杂:其中涉及到了通过递归函数计算斐波那契数列,然后将数列的前10个值打印出来。

运行脚本:

$ got ./examples/fib.gotmpl
0: 0
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34

语法高亮(VS Code)

安装 gotemplate-syntax 插件,效果如下:

版权

狗头语 版权 @2019 柴树杉。

Core symbols most depended-on inside this repo

builtinFuncMap
called by 1
builtins.go
NewGotApp
called by 1
app.go
Run
called by 1
app.go
buildTemplate
called by 1
app.go
loadProgram
called by 1
got/main.go
fn_print
called by 0
builtins.go
fn_printf
called by 0
builtins.go
fn_println
called by 0
builtins.go

Shape

Method 30
Function 4
Struct 1

Languages

Go100%

Modules by API surface

builtins.go28 symbols
app.go4 symbols
got/main.go3 symbols

For agents

$ claude mcp add gotlang \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page