百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pytago

> AI 编程
开源

一个用于 Python 到 Go 翻译的源到源转换器

396 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个用于 Python 到 Go 翻译的源到源转换器

Pytago is a source-to-source transpiler that converts some Python into human-readable Go.

Installation and usage

There are two "officially" supported ways to use Pytago:

  1. A web application you can run via docker
  2. A locally-installed command-line tool

Web application

Prerequisites

  • Docker

Installation and Usage

Running pre-built container
bash
docker run -p 8080:8080 -e PORT=8080 -it nottheswimmer/pytago
Building from source (Alternative)
bash
git clone https://github.com/nottheswimmer/pytago/
cd pytago
docker build -t pytago .
docker run -p 8080:8080 -e PORT=8080 -it pytago
Usage
bash
# Open the user interface
open http://127.0.0.1:8080/

# API
curl --request POST 'http://127.0.0.1:8080/' \
  --header 'Content-Type: application/json'  \
  --data-raw '{"py": "print(\"Hello World\")"}'

Local command-line application

Prerequisites

  • Go 1.18.x
  • Python 3.10.x
    • No, it will not work on 3.9 due to the (heavy) usage of match statements.
  • Required libraries for post-processing:
    bash
    go install golang.org/x/tools/cmd/goimports@latest
    go install mvdan.cc/gofumpt@latest
    go install github.com/segmentio/golines@latest
    export PATH="$PATH:$HOME/go/bin"

Installation

via pip
bash
pip install pytago
via setup.py (dev)
bash
git clone https://github.com/nottheswimmer/pytago/
cd pytago
pip install -e .
via setup.py
bash
git clone https://github.com/nottheswimmer/pytago/
cd pytago
pip install .

Usage

usage: pytago [-h] [-o OUTFILE] INFILE

positional arguments:
  INFILE                read python code from INFILE

options:
  -h, --help            show this help message and exit
  -o OUTFILE, --out OUTFILE
                        write go code to OUTFILE

Examples

All examples presented here are used as tests for the program.

listappend

PythonGo

python
def main():
    a = [1, 2, 3]
    a.append(4)
    print(a)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := []int{1, 2, 3}
	a = append(a, 4)
	fmt.Println(a)
}

abs

PythonGo

python
def main():
    print(abs(-6))
    print(abs(3))

if __name__ == '__main__':
    main()
go
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Abs(-6))
	fmt.Println(math.Abs(3))
}

helloworld

PythonGo

python
def main():
    print("hello world")

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

input

PythonGo

python
def main():
    print("Hi, what's your name?")
    name = input("Name: ")
    print("Hi", name, "how old are you?")
    age = int(input("Age: "))
    print("Describe yourself in one sentence:")
    description = input()
    print("So your name is", name, "and you are", age, "years old, and your description is", '"' + description + '"')

if __name__ == '__main__':
    main()
…

randomness

PythonGo

…
…

list_methods

PythonGo

…
…
go
package main

import "fmt"

func main() {
	fmt.Println(add(2, 2))
}

func add(a int, b int) int {
	return a + b
}

exponents

PythonGo

python
def main():
    print(2 ** 8)
    x = [2, 4]
    print(x[0] ** x[1])

if __name__ == '__main__':
    main()
go
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Pow(2, 8))
	x := []int{2, 4}
	fmt.Println(math.Pow(float64(x[0]), float64(x[1])))
}

variables

PythonGo

python
def main():
    a = 3
    b = 7
    a = a + b
    print(a + b)
    another_scope()

def another_scope():
    a = 1
    b = 12
    a = a + b
    print(a + b)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := 3
	b := 7
	a = a + b
	fmt.Println(a + b)
	another_scope()
}

func another_scope() {
	a := 1
	b := 12
	a = a + b
	fmt.Println(a + b)
}

floats

PythonGo

python
def main():
    a = 7
    b = 3
    c = 4.5
    print(a / b)
    print(a // b)
    print(a / c)
    print(a // c)
    print(a + b)
    print(a + c)

if __name__ == '__main__':
    main()
go
package main

import (
	"fmt"
	"math"
)

func main() {
	a := 7
	b := 3
	c := 4.5
	fmt.Println(float64(a) / float64(b))
	fmt.Println(a / b)
	fmt.Println(float64(a) / c)
	fmt.Println(math.Floor(float64(a) / c))
	fmt.Println(a + b)
	fmt.Println(float64(a) + c)
}

numlist

PythonGo

python
def main():
    a = [1, 2, 3]
    print(a[0])
    print(a[1])
    print(a[2])
    a.append(4)
    print(a[3])
    a += [5, 6, 7]
    a = a + [8, 9, 10]
    print(a[4])
    print(a[-1])

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := []int{1, 2, 3}
	fmt.Println(a[0])
	fmt.Println(a[1])
	fmt.Println(a[2])
	a = append(a, 4)
	fmt.Println(a[3])
	a = append(a, []int{5, 6, 7}...)
	a = append(a, []int{8, 9, 10}...)
	fmt.Println(a[4])
	fmt.Println(a[len(a)-1])
}

loops

PythonGo

python
def main():
    a = [1, 2, 3]
    for v in a:
        print(v)

    for i, v in enumerate(a):
        print(i + v)

    for i in range(5):
        print(i)

    for i in range(10, 15):
        print(i)

    for i in range(10, 15, 2):
        print(i)

    for j in range(15, 10, -1):
        print(j)

    for j in range(15, 10, -2):
        print(j)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := []int{1, 2, 3}
	for _, v := range a {
		fmt.Println(v)
	}
	for i, v := range a {
		fmt.Println(i + v)
	}
	for i := 0; i  10; j-- {
		fmt.Println(j)
	}
	for j := 15; j > 10; j -= 2 {
		fmt.Println(j)
	}
}

strings

PythonGo

python
def main():
    a = "hello"
    b = "world"
    c = a + " " + b
    print(c)
    print(double_it(c))
    print(c[1])
    print(c[1:6])

def double_it(c: str) -> str:
    return c + c

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := "hello"
	b := "world"
	c := a + " " + b
	fmt.Println(c)
	fmt.Println(double_it(c))
	fmt.Println(string(c[1]))
	fmt.Println(c[1:6])
}

func double_it(c string) string {
	return c + c
}

logical

PythonGo

python
def main():
    a = True
    b = False
    print(a)
    print(b)
    print(a and b)
    print(a or b)
    print(not a)
    print(not b)
    print(a and not b)
    print(a or not b)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := true
	b := false
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(a && b)
	fmt.Println(a || b)
	fmt.Println(!a)
	fmt.Println(!b)
	fmt.Println(a && !b)
	fmt.Println(a || !b)
}

maths

PythonGo

python
import math

def main():
    print(math.sin(3))
    print(math.cosh(3))
    print(math.pi)
    print(math.acosh(6))
    print(math.atan2(4, 7))

if __name__ == '__main__':
    main()
go
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Sin(3))
	fmt.Println(math.Cosh(3))
	fmt.Println(math.Pi)
	fmt.Println(math.Acosh(6))
	fmt.Println(math.Atan2(4, 7))
}

requestslib

PythonGo

python
import requests

def main():
    resp = requests.get("http://tour.golang.org/welcome/1")
    print(resp.text)

if __name__ == '__main__':
    main()
go
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	resp, err := http.Get("http://tour.golang.org/welcome/1")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println(func() string {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			panic(err)
		}
		return string(body)
	}())
}

conditionals

PythonGo

python
def main():
    a = 7
    b = add(a, -2)
    if a > b:
        print("It's bigger")
    elif a == b:
        print("They're equal")
    else:
        print("It's smaller")

def add(a: int, b: int) -> int:
    return a + b

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	a := 7
	b := add(a, -2)
	if a > b {
		fmt.Println("It's bigger")
	} else if a == b {
		fmt.Println("They're equal")
	} else {
		fmt.Println("It's smaller")
	}
}

func add(a int, b int) int {
	return a + b
}

fstrings

PythonGo

python
def main():
    name = "Michael"
    age = 24
    print(f"My name is {name} and I am {age} years old. Later this year I'll be {age + 1}!")

if __name__ == '__main__':
    main()
go
package main

import (
	"bytes"
	"fmt"
	"text/template"
)

func main() {
	name := "Michael"
	age := 24
	fmt.Println(func() string {
		var buf bytes.Buffer
		err := template.Must(template.New("f").Parse("My name is {{.name}} and I am {{.age}} years old. Later this year I'll be {{.expr1}}!")).
			Execute(&buf, map[string]interface{}{"name": name, "age": age, "expr1": age + 1})
		if err != nil {
			panic(err)
		}
		return buf.String()
	}())
}

nestedfstrings

PythonGo

python
def main():
    name = "Michael"
    age = 24
    print(f"My name is {name} and I am {age} years old. Later this year I'll be {f'{age + 1}'}!")

if __name__ == '__main__':
    main()
…

dictionary

PythonGo

python
def main():
    a = {"name": "Michael", "age": 24, 1337: True}
    print(a)
    a["sleepiness"] = 1.0
    del a[1337]

    for k, v in a.items():
        print(k)
        print(v)
go
package main

import "fmt"

func main() {
	a := map[interface{}]interface{}{"name": "Michael", "age": 24, 1337: true}
	fmt.Println(a)
	a["sleepiness"] = 1.0
	delete(a, 1337)
	for k, v := range a {
		fmt.Println(k)
		fmt.Println(v)
	}
}

writefile

PythonGo

…
…

pass

PythonGo

python
def main():
    pass

if __name__ == '__main__':
    main()
go
package main

func main() {
}

ellipsis

PythonGo

python
def main():
    ...

if __name__ == '__main__':
    main()
go
package main

func main() {
}

missingreturntype

PythonGo

python
def main():
    print(add(1, 3))

def add(a: int, b: int):
    return a + b
go
package main

import "fmt"

func main() {
	fmt.Println(add(1, 3))
}

func add(a int, b int) int {
	return a + b
}

continuestmt

PythonGo

python
def main():
    for i in range(10):
        if i  7:
            continue
        print(i)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	for i := 0; i  7 {
			continue
		}
		fmt.Println(i)
	}
}

breakstmt

PythonGo

python
def main():
    for i in range(10):
        if i > 7:
            break
        print(i)

if __name__ == '__main__':
    main()
go
package main

import "fmt"

func main() {
	for i := 0; i  7 {
			break
		}
		fmt.Println(i)
	}
}

whileloop

PythonGo

…
…

globals

PythonGo

python
SITE = "https://www.google.com/"
NAME = ["Michael", "Wayne", "Phelps"]
KEYS = {1: 2, 3: 4}
AGE = 1000
BIRTH_YEAR = 2050

def main():
    global AGE
    print(SITE)
    print(NAME)
    print(BIRTH_YEAR)
    print(KEYS)
    print(AGE)
    AGE = 20  # This should use the variable from the global scope
    other_1()
    print(AGE)
    other_2()

def other_1():
    AGE = 200  # This should declare a new variable age
    print(AGE)

def other_2():
    print(AGE)  # This should still be able to access the global

if __name__ == '__main__':
    main()
go
package main

import "fmt"

var (
	SITE       = "https://www.google.com/"
	NAME       = []string{"Michael", "Wayne", "Phelps"}
	KEYS       = map[int]int{1: 2, 3: 4}
	AGE        = 1000
	BIRTH_YEAR = 2050
)

func main() {
	fmt.Println(SITE)
	fmt.Println(NAME)
	fmt.Println(BIRTH_YEAR)
	fmt.Println(KEYS)
	fmt.Println(AGE)
	AGE = 20
	other_1()
	fmt.Println(AGE)
	other_2()
}

func other_1() {
	AGE := 200
	fmt.Println(AGE)
}

func other_2() {
	fmt.Println(AGE)
}

asyncawait

PythonGo

python
import asyncio

async d

Issues· 12 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonabstract-syntax-treeastcodegencompiler

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类AI 编程
定价开源

> 相关工具

G
GitHub Copilot
GitHub 官方 AI 编程助手,覆盖补全、Chat 与 Agent 模式。
C
Cursor
AI 原生代码编辑器,对话改代码、多文件 Agent 与规则体系是其核心。
S
skills
Skills for Real Engineers. Straight from my .agents directory.