How I Use chatGPT to Write a Program in Golang
There is no doubt chatGPT has been one of the hottest topics in the past few months. I was not impressed with the answers I got when it was first released.
For example, here is the answer I got for asking how does nginx ingress controller passthrough mode work?.
This is from official document.
As we can see, chatGPT clearly got it wrong but it was so confident with its answer. We already have too many people talking about the things they don’t know, I really do not need more incorrect information so I stopped using it after that.
After getting the beta access to Google Bard, I decided to compare these two. I happen to have a task to find out the number of entities for a Kong Enterprise installation. I already wrote a shell script to get this data and I thought it would be a fun test to see if chatGPT and Bard can write the same function in Go instead.
So I asked the same questions on both platforms. My initial requests were:
- Send HTTP request to
localhost:8001/workspaces
to get workspace information. (I also fed response JSON to them)- Add a flag to specify
url
, if not found,url
falls back tolocalhost:8001
This can’t be easier but only the code from chatGPT worked…🥴
At this point, I did not bother testing Bard further so I only focused on chatGPT. After spending a couple hours feeding question, data and errors, chatGPT was able to give me the code that output the data I need.
Besides the fact that Golang is much faster (expected), chatGPT can also integrate third party library to satisfy my needs for example printing data in a table.
Here is the speed comparison:
Shell Script
1
2
3
4
5
6
7
8
9➜ time ./meta.sh
Entity Name Total Number
Workspaces 20
Upstreams 100
Targets 200
Plugins 1500
Services 4500
Routes 8000
./meta.sh 3.42s user 0.22s system 96% cpu 3.764 totalGolang
1
2
3
4
5
6
7
8
9
10
11
12
13➜ time ./meta
Total Meta Field Counts:
+------------+-------+
| META FIELD | COUNT |
+------------+-------+
| Workspaces | 20 |
| upstreams | 100 |
| targets | 200 |
| plugins | 1500 |
| services | 4500 |
| routes | 8000 |
+------------+-------+
./meta 0.01s user 0.00s system 5% cpu 0.162 total
This might look amazing but there are a few things you need to understand before starting using chatGPT to write code.
- chatGPT can’t handle complex issue.
- You need to know exactly what you want to do in each step.
- Code Quality is not verified.
Most importantly, you can’t learn a programming language by using chatGPT. You can ONLY learn from your mistakes so please get your hands dirty.
Anyway, I have put the generated code on GitHub, feel free to check it out.
I hope you find this post interesting and see you on the next one.