Go code with ChatGPT
Using AI to write idiomatic Go
Since a few months, as I am in the process of switching career from DevOps/SRE to Backend Engineering, I have started to improve my Golang knowledge and learn Backend development skills. I am a big fan of podcasts, and so I have started to listen to the excellent Backend Banter Podcast. A few days ago, I stumbled upon an episode featuring Natalie Pistunovich. She is an OpenAI Ambassador and Google Developer Expert for Go, and so I wasn’t surprised the interview focused on using ChatGPT when coding in Go. The episode is really informative, and I suggest everybody interested in using ChatGPT for code generation, and particularly to generate Go code, to listen to it. But the episode left me wondering: how good would ChatGPT be in converting existing code from another language to Go?
I am interested also in open social networks, both ActivityPub (used by Mastodon) and the recently released ATProto (used by BlueSky). While both platforms are fascinating and have a bright future, I am leaning towards BlueSky lately, both technically (ATProto allow true nomadic profiles) and regarding the content on the networks (Mastodon is having an antisemitic problem since the Israel-Gaza war). Anyway, while studying the ATProto protocol and APIs, I have found this example Python code, which allows posting to BlueSky from the command line. The code is simple and concise, and so I thought it was going to be a great test to see how well ChatGPT can translate code from one language to another.
The first step I took was to feed ChatGPT the Python code and ask it to generate the equivalent Go 1.22 code. The code generated compiled, and it worked without issues. As you can see, the code was organized in a nice way, with the structs and variables on the top of the listing, followed by the functions. Interestingly, the imports have been converted in an intelligent way, using the Go Standard Library instead of adding external dependencies.
The second step I took was to ask ChatGPT to break the code into modules, generating idiomatic Go and using best practices when splitting the code. Again, ChatGPT generated working code, which compiled without errors or warnings. The code was split in logical modules, producing an easily understandable structure. Interestingly, a few external dependencies were added to the go.mod file, however they were not used; a quick go mod tidy cleaned up this “issue”.
My last step was to experiment with adding features to my code. I asked ChatGPT to add a parameter to allow customizing the published date of a post. This time the code did not fit the original structure. The amended PostToBsky() function was added to the wrong package, and the code was not consistent with the existing code. A quick guided fix later (thanks VSCode!) and the code compiled. Then I found the issue. When asking to generate the new feature, I didn’t specify Go version 1.22. Interesting. Another interesting difference was the use of fmt.Printf instead of log.Fatalf, which needed a manual fix as well. Not a breaking problem, but not consistent with the rest of the code generated.
Another point to notice is that not all types were placed in their dedicated file (pkg/utils/types.go ). I did notice two struct in the file containing utility functions (pkg/utils/utils.go ). Another manual fix and all looks proper. All considered, I am very happy with the experiment. ChatGPT managed to produce not only working code, but code that looks idiomatic and manage all error returns, which often is ignored when writing Go code.
To conclude, the lesson is that ChatGPT can produce great code, and can be extremely helpful when translating old code from a programming language to another. Especially Go code, with its opinionated approach, its enforced layout and limitations in personal style, is a perfect target for code generation; perhaps with a less opinionated language like C the story would have been different. Obviously, the code generated should not be taken at face value; an expert look is needed, and some manual adjustments are still necessary both to organize and make all functions aligned in some details. In more complex cases, more in-depth code reading is a must. However, the use of AI can tremendously speed up translating and refactoring old code, either to take advantage of a new programming language or in the case of lacking expertise to manage old code (hello COBOL and Visual Basic code bases!). In any case, black-box testing is a necessary step to take before releasing in production such converted code. But the boost in productivity cannot be ignored. I have a few years of experience writing Go code, but very little knowledge of Python (I can “glue” together some APIs if needed, but nothing longer than a few tens or lower hundreds lines of code). Manually translating longer code bases, or rewriting from scratch a more complex solution, would be more time consuming. Using AI to assist can allow massive time savings that can be reinvested in improving the translated code and use our expertise in with a known language and environment.