

The first step is to initialize a new module in your Go project. Step 1: Initialize a new module in your project
Import shorthand into swifttext how to#
“ How to install Golang in 5 easy steps“. If you haven’t installed Go and setup your $GOPATH, please read this guide before proceeding. Create a new directory for different packages.įrom here on out, I will be referring a directory as a Go package. In a directory you cannot have multiple package names in Go. You can have a directory called utils, but the files inside can use a package name called silly_goose. It’s common practice to do that, but you don’t have to. In the example above I defined the package name in the Go file the same as the directory name. To recap, a directory inside a project is known as a Go package. You just import the package and you have access to the all the exported code within that package. That means you don’t have to import single files. In the example, init.go is assigned to the woosah Go package.Īnd the file a.go, is assigned to the bar Go package.Įvery exported code inside a Go package is automatically accessible when importing. So the file main.go in the example above is assigned to the main package. The root of a project is always known as the main package.

Every file inside that directory is automatically assigned to that directory aka a Go package. In Golang, a directory is known as a package. Inside my project I have 2 more directories called woosah and bar. Let’s say the project directory is called. What is a Go package?Ī Go package also known as a “workspace”, is just a directory in your project. If you already know feel free to jump to step 1. This article will cover everything you need to know about how to import a local Go files into your project.īefore I go through the step-by-step process, you need to understand what a Go package means. But jumping into Golang, I struggled for hours trying to figure out how to import a local file and finding good resources that explain how it works was another pain point.
