Code like a PIRATE with Junie and GoLand
Code like a PIRATE with Junie and GoLand
This is a guest post from John Arundel, a Go writer and teacher who runs a free email course for Go learners. His most recent book is The Deeper Love of Go.
Ahoy, maties! Capân Long John Arundel here with more tips on sailing the good ship GoLand. This time, weâll lay aloft to the crowâs nest and turn our spyglass on Junie, the JetBrains AI coding agent.
If youâre new to Junie and AI tools, and arenât sure where to start, think of this as your treasure map to the hidden gold of GoLand productivity. Arrr you ready to start coding like a pirate?
Flying the black flag
Your first voyage with AI development tools can be a perilous one, veering from calm seas of code to storms of syntax errors and test failures. So, will your AI agent be a trusty first mate, or just an unpredictable stochastic parrot squawking nonsense from your shoulder?
Junie is pretty smart, and she can tackle any task you choose, but she needs the guidance of a good capân. To help you stay on course, Iâve put together a handy six-step workflow I like to call âCode like a PIRATEâ. Aye, âtis another oâ my made-up acronymsâbut yeâll find it easy to remember, me hearties [Are we doing the pirate thing for the whole article?âAnna]
âPâ is for Plan
Every good voyage begins with a map. When you set Junie a task, tell her where the shipâs headed, so sheâll know which direction to steer. (Iâm sure Junie wonât mind me calling her âsheâ; fun fact, lots of pirates were womenâincluding quite a few of the men).
Ask Junie to draw up a quick chart for the voyage, but not to set sail until youâve approved it:
Arr, Junie, me fine lass. Iâm a swashbuckling pirate capân who needs a Go program to help me share out the booty from my latest captured Spanish galleon. Donât code anything yet, but show me a brief plan of how the tool might work.
Itâs important to get this stuff right before you even leave the dock, so donât be afeared to spend a bit of time refining the plan. [Iâm âafearedâ we might be wearing out the salty sea-dog bit alreadyâAnna]
âIâ is for Iterate
Even the boldest captains donât try to cross an ocean in a single leap. Itâs more effective to island-hop, sailing a short distance at a time and checking youâre still on course. Give Junie one small task at a time, starting with the simplest possible program that could be useful:
Letâs start with a really simple prototype. Iâd like to be able to run the âbootyâ calculator and answer two questions: the number of crew, and the number of pieces of eight to be divided among them.
Assume everyone has an equal share. The tool should print out how much each crew member is due. Write just enough code to achieve this, and then weâll think about the next stage.
With too vague a heading, Junie can end up going a bit adrift, like any of us: donât hesitate to cry âAvast heaving there!â and interrupt her if that happens. Rowing back when the project has gone too far in the wrong direction will cost you a lot of time and doubloons [Seriously, clap a stopper on the pirate speak for nowâAnna]. Sorry, I meant to say âtokensâ.
âRâ is for Review
Once Junie has completed each iteration, go through the code line by line to check and review her work. Sheâs pretty good at delivering what you asked for, but she doesnât necessarily know how you want it. For example:
Nice job, Junie, but I have a few suggestions for improvement.
- Instead of creating a
bufio.Scanner
in main and passing a pointer to it into theaskInt
function, letâs eliminate some of that paperwork. ChangeaskInt
to take just the prompt string, and have it create the scanner internally. - The
askInt
function shouldnât print error messages and callos.Exit
if thereâs a scan error. Instead, have it return any error along with the integer result. Let main take care of all the printing and exiting. - If thereâs an error from
strconv.Atoi
, include the invalid input in the error message. For example, âSorry, I didnât understand%q
. Please enter a whole number.â - Move the shares calculation into its own function, so that we decouple the input/output code from the business logic. Have it return the share and remainder values, so that main can print them out.
When giving feedback, bundle all your comments together in one message. This lets Junie generate the new version of the program in a single step, saving tokens. If you keep making small comments and asking her to rebuild the whole program each time, youâll find your pieces of eightâI mean, creditsâdwindling rapidly.
Good programs have a harmonious architecture that makes overall sense: everything works the same way everywhere and it all seems to fit together neatly. Junie canât achieve this without your guidance, so keep a hand on the tiller and help her ensure things slot neatly into a unified structure.
âAâ is for Assess
Once Junie has finished the step you asked for, donât just glance at the code and move onâtake a moment to assess whether it actually does what it should. Does the program run cleanly? Do the functions behave as expected? Are there strange side effects lurking in the bilges, waiting to sink your ship later? [What did I just say?âAnna]
Now that you can see the program in action, you might realise itâs not quite what you want. If so, nowâs the time to adjust course, either with Junieâs help or by making little steering inputs yourself.
If youâre happy with the assessment, though, you can move on to the next iterative step towards the final program:
Shiver me timbers, Junie, that be some fine work.
Could you now please move the business logic functions into a booty package in the project root, and put the main.go file into a cmd/booty subfolder?
Also, could ye change the plunder calculations so that the captain gets twice the share of a regular crewmember? Print out the captainâs share separately.
âTâ is for Test
No old salt trusts a ship that hasnât been through its sea-trials, and nor should you. As you and Junie build the program, check each new plank is watertight by adding tests to accompany each function. That way, youâll know as soon as something springs a leak.
Arr, please add some unit tests now for the CalculateShares function. Generate at least ten test cases.
Move the askInt function into the booty package too, and add logic to check that the number entered is always 1 or greater, or return an appropriate error if itâs not.
Have the function take an io.Reader
to read input from, and an io.Writer
to print prompts to.
Generate two tests for this function, one for valid inputs, one for invalid inputs.
Junie can be a helpful shipmate when it comes to drafting tests, but donât just accept her handiwork blindly. Ask yourself: What is this really testing? Are there hidden reefsâedge casesâthat weâre missing? And, when the tests fail (theyâre no use otherwise) do they print something helpful?
Tis a fine set of tests ye have there, Junie. Could you make them all run in parallel?
In the table tests, could you use a map of test cases keyed by name, and then use t.Run in the test loop with the map key as the subtest name? Thatâll make it easier on any scurvy dogs trying to understand the failure output.
Donât try to inspect the error string itself for invalid inputs; that leads to fragile tests. Instead, just check that AskInt returns any non-nil error for these cases.
âEâ is for Evaluate
Machine learning is fine, but human learning is even better. After each task, take a little time to analyse what worked, and what could have gone better.
Were your prompts detailed enough? Did Junie sail safely into harbour, or did she end up grounded on a sandbar because her pilot was too busy splicing the mainbrace? Every voyage is a lesson thatâll help you sharpen your prompting skills, anticipate pitfalls, and become a steadier pirate capân for the next expedition.
If you remember the chart weâve drawn here and use it to navigate your next project, with the help of Junie and GoLand, youâll be ready to truly code like a PIRATE [Thatâs it, youâre walking the plankâAnna].
Anchors aweigh
Check out the booty calculator project to see what Junie and I built togetherâtry using it to divvy up your own pirate booty with friends. Itâs also kind of fun to say âbootyâ.
Until next time, shipmates, wishinâ ye fair winds and full sails!
Legal disclaimer: JetBrains s.r.o. does not advocate piracy, illegal seizure of vessels on the high seas, or the consumption of rum. Please swashbuckle responsibly.
How it works
Once you click Generate, Ollama reads this article and crafts 5 comprehension questions. Your answers are graded against the article content â general knowledge won't be enough. Score 70+ to count toward your certificate.
Questions are cached â you'll always get the same 5 for this article.