Learn how to solve CodeCrafters challenges in Go
go
installed. Our test runners use go
version 1.19
(as of November 2023).
The script for your challenge (like ./your_bittorrent.sh
) will automatically compile your code using go build
before executing it.
app/
directory.app/main.go
contains the main
function, which is what the test runner executes.app/
. The test runner will include them when compiling your code.
For example, if you added a file at app/foo.go
, you could use it like so:
app/
must have a package main
declaration at the top of the file. If you want to extract files into a different package,
you can create a new folder in app/
and add the package
declaration to the top of each file in that folder.
Let’s say you wanted to extract foo.go
into the bar
package. You’d move foo.go
into the app/bar
and add the relevant package
declaration to the top of foo.go
:
main.go
to use the new package name (Example: github.com/codecrafters-io/dns-server-starter-go/app/bar
). You’ll
find the first part of the package name in go.mod
.
go.mod
file. You can use the go get
command for this.
For example, to add the github.com/spf13/cobra
library, you can run the following command:
go.mod
file:
go.sum
file.
When you push your code next, the runner will automatically download the dependency.
You can use the dependency in your code like so: