一个用于 Python 到 Go 翻译的源到源转换器
Pytago is a source-to-source transpiler that converts some Python into human-readable Go.
There are two "officially" supported ways to use Pytago:
docker run -p 8080:8080 -e PORT=8080 -it nottheswimmer/pytagogit clone https://github.com/nottheswimmer/pytago/
cd pytago
docker build -t pytago .
docker run -p 8080:8080 -e PORT=8080 -it pytago# 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\")"}'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"pip install pytagogit clone https://github.com/nottheswimmer/pytago/
cd pytago
pip install -e .git clone https://github.com/nottheswimmer/pytago/
cd pytago
pip install .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 OUTFILEAll examples presented here are used as tests for the program.
PythonGo
def main():
a = [1, 2, 3]
a.append(4)
print(a)
if __name__ == '__main__':
main()package main
import "fmt"
func main() {
a := []int{1, 2, 3}
a = append(a, 4)
fmt.Println(a)
}PythonGo
def main():
print(abs(-6))
print(abs(3))
if __name__ == '__main__':
main()package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Abs(-6))
fmt.Println(math.Abs(3))
}PythonGo
def main():
print("hello world")
if __name__ == '__main__':
main()package main
import "fmt"
func main() {
fmt.Println("hello world")
}PythonGo
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()…PythonGo
……PythonGo
……package main
import "fmt"
func main() {
fmt.Println(add(2, 2))
}
func add(a int, b int) int {
return a + b
}PythonGo
def main():
print(2 ** 8)
x = [2, 4]
print(x[0] ** x[1])
if __name__ == '__main__':
main()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])))
}PythonGo
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()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)
}PythonGo
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()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)
}PythonGo
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()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])
}PythonGo
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()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)
}
}PythonGo
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()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
}PythonGo
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()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)
}PythonGo
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()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))
}PythonGo
import requests
def main():
resp = requests.get("http://tour.golang.org/welcome/1")
print(resp.text)
if __name__ == '__main__':
main()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)
}())
}PythonGo
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()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
}PythonGo
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()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()
}())
}PythonGo
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()…PythonGo
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)
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)
}
}PythonGo
……PythonGo
def main():
pass
if __name__ == '__main__':
main()package main
func main() {
}PythonGo
def main():
...
if __name__ == '__main__':
main()package main
func main() {
}PythonGo
def main():
print(add(1, 3))
def add(a: int, b: int):
return a + bpackage main
import "fmt"
func main() {
fmt.Println(add(1, 3))
}
func add(a int, b int) int {
return a + b
}PythonGo
def main():
for i in range(10):
if i 7:
continue
print(i)
if __name__ == '__main__':
main()package main
import "fmt"
func main() {
for i := 0; i 7 {
continue
}
fmt.Println(i)
}
}PythonGo
def main():
for i in range(10):
if i > 7:
break
print(i)
if __name__ == '__main__':
main()package main
import "fmt"
func main() {
for i := 0; i 7 {
break
}
fmt.Println(i)
}
}PythonGo
……PythonGo
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()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)
}PythonGo
import asyncio
async d暂无开放 Issues,或尚未同步最近议题。