|
有时候我们需要将一种编程语言的代码转换成另一种编程语言的代码。这可能是因为某些原因,如项目需求、团队合作、代码性能等。而通过使用ChatGPT,我们可以实现快速高效的代码语言转换。网上有一个开源的项目【1】封装了 ChatGPT API,简单的通过了代码转换。
核心的代码逻辑是,Prompts 的编写。我这里简单的分析了一下源代码,如下:
You are an expert programmer in all programming languages. Translate the "${inputLanguage}" code to "${outputLanguage}" code. Do not include \\`\\`\\`.
Example translating from JavaScript to Python:
JavaScript code:
for (let i = 0; i < 10; i++) {
console.log(i);
}
Python code:
for i in range(10):
print(i)
${inputLanguage} code:
${inputCode}
${outputLanguage} code (no \\`\\`\\`):
我们可以通过在 ChatGPT 简单的引用上面的 Prompt。以 Python 代码转换成 Goland 代码为例子:
You are an expert programmer in all programming languages. Translate the python code to Goland code. Do not include \`\`\`.
Example translating from JavaScript to Python:
JavaScript code:
for (let i = 0; i < 10; i++) {
console.log(i);
}
Python code:
for i in range(10):
print(i)将 Python 转换成 Go 代码(文本进度条代码功能)
import time
scale=50
print(&#34;starting&#34;.center(scale//2,&#34;-&#34;))
start=time.perf_counter()
for i in range(scale+1):
a=&#39;*&#39;*i
b=&#39;.&#39;*(scale-i)
c=(i/scale)*100
dur=time.perf_counter()-start
print(&#34;\r{:^3.0f}%[{}->{}]{:.2f}s&#34;.format(c,a,b,dur),end=&#34;&#34;)
time.sleep(0.1)
print(&#34;\n&#34;+&#34;ending&#34;.center(scale//2,&#39;-&#39;))ChatGPT 生成了 Go 代码
package main
import (
&#34;fmt&#34;
&#34;time&#34;
)
func main() {
scale := 50
fmt.Println(&#34;starting&#34;.center(scale/2, &#39;-&#39;))
start := time.Now()
for i := 0; i < scale+1; i++ {
a := &#34;&#34;
for j := 0; j < i; j++ {
a += &#34;*&#34;
}
b := &#34;&#34;
for j := i; j < scale; j++ {
b += &#34;.&#34;
}
c := (float64(i) / float64(scale)) * 100
dur := time.Since(start).Seconds()
fmt.Printf(&#34;\\r%3.0f%%[%s->%s]%.2fs&#34;, c, a, b, dur)
time.Sleep(time.Millisecond * 100)
}
fmt.Println(&#34;\\n&#34; + &#34;ending&#34;.center(scale/2, &#39;-&#39;))
}本地运行会提示:./1.go:10:28: &#34;starting&#34;.center undefined (type untyped string has no field or method center)
让 ChatGPT 自己 fix 自己的代码
将错误再一次问 ChatGPT,ChatGPT 会更正之前生成的代码,给一个新的代码。

package main
import (
&#34;fmt&#34;
&#34;strings&#34;
&#34;time&#34;
)
func main() {
scale := 50
fmt.Println(strings.Repeat(&#34;-&#34;, scale/2) + &#34;starting&#34; + strings.Repeat(&#34;-&#34;, scale/2))
start := time.Now()
for i := 0; i < scale+1; i++ {
a := strings.Repeat(&#34;*&#34;, i)
b := strings.Repeat(&#34;.&#34;, scale-i)
c := (float64(i) / float64(scale)) * 100
dur := time.Since(start).Seconds()
fmt.Printf(&#34;\\r%3.0f%%[%s->%s]%.2fs&#34;, c, a, b, dur)
time.Sleep(time.Millisecond * 100)
}
fmt.Println(&#34;\\n&#34; + strings.Repeat(&#34;-&#34;, scale/2) + &#34;ending&#34; + strings.Repeat(&#34;-&#34;, scale/2))
}验证结果
Python 语言的代码成功转换成 Go 了。如果你想体验开源版本的,可以直接使用 AI Code Translator【2】 去做体验。
$ python3 t.py
---------starting--------
100%[**************************************************->]5.15s
----------ending---------
$ go run t.go
-------------------------starting-------------------------
100%[**************************************************->]5.07s
-------------------------ending-------------------------总结
- ChatGPT 并不能完全生成我们需要的代码。关于 ChatGPT 能否替代程序员,可以参考我之前写的一篇。
- 好的 Prompt 是使用 ChatGPT 的关键。它可以更好地为我们需要的场景提供服务。
如果您有任何想法,欢迎留言评论并关注。通过关注我,您可以更及时地了解 ChatGPT 相关 AI 话题的最新进展。
参考资料
[1] https://github.com/mckaywrigley/ai-code-translator
[2]https://ai-code-translator.vercel.app/ |
|