New to Go? We recommend checking out Go by Example to get up to speed.
Local development
To run the code for your challenge locally, you’ll needgo 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.
File structure
- The files for your solution are placed in the
app/directory. app/main.gocontains themainfunction, which is what the test runner executes.
Adding more files
You can add more files and directories toapp/. 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.
Adding dependencies
You can add dependencies to your project by adding them to yourgo.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: