# Modules & Packages

## Packages

Every Go application needs at least one `package`, that you can split your code in.

<img src="https://1500218687-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCP9oXFgvjfiSPa5uKymZ%2Fuploads%2F5uny8bHBKK6USPlNN7Fk%2Ffile.excalidraw.svg?alt=media&#x26;token=6b5e6e50-f1bf-444a-9efa-ce2cd8db2b09" alt="" class="gitbook-drawing">

Usually you will have a default `package main`. Theoretically it could be any package name, as long as all files belong to the same package.

{% hint style="danger" %}
`main` is a special name for Go that tells it that this package will be the main entrypoint of the application.

* In developing environments when executing the application with `go run app.go` you specify which file will be the entrypoint.
* But for production environments, where the code is compiled to be runned, everything will be mixed together in the binary file, so the entrypoint from the package name is important.
  {% endhint %}

{% hint style="warning" %}
Compiling a project without a `main` package produces no executable file.
{% endhint %}

You may also split your code into multiple packages.

<img src="https://1500218687-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCP9oXFgvjfiSPa5uKymZ%2Fuploads%2FlWVsRcunPC7PxluJnswi%2Ffile.excalidraw.svg?alt=media&#x26;token=562a3a20-aa4a-4174-8a6b-99f413e84ffa" alt="" class="gitbook-drawing">

### Importing own packages

```go
// Your own packages
import "your-module/package-name"

// Third-party that were downloaded with `go get`
import "https://package-url.com"
```

{% hint style="danger" %}
Variables, constants, functions, etc, **are ONLY available (exported) to other packages** if they start in `Uppercase`.

Ex.:

* `GetSomething()`: this function will be available in other packages.
* `getSomething()`: this function is "private" to the package that it is declared.
  {% endhint %}

### Add packages locally

This downloads the package and stores globally on your system.

```bash
go get https://package-url.com
```

### Install packages

Just like `npm install` run bellow to install the projects dependencies.

{% hint style="info" %}
It fetches the current required version (defined in your `go.mod` or latest compatible)

It not listed uet, it adds the latest matching your current module's Go version and requirements.

It does not update transitive dependencies unless needed.

**It fetches a version that works with the rest of your dependencies. No wild upgrades.**
{% endhint %}

```bash
go get
```

{% hint style="info" %}
Fetches the latest available version of that package and all its dependencies (upgrades them!)

Can cause major upgrades to transitive deps.

**Will pull the latest package AND the latest of all its nested imports (could affect other parts of your codebase)**
{% endhint %}

```bash
go get -u
```

## Modules

A module consist of multiple packages, and it is the `go.mod` file in your project.

{% hint style="warning" %}
Initiate the module in your project, or else, you will not be able to compile your project.
{% endhint %}

```wasm
go mod init <path>
```

The `path` can be a name or dummy URL, or even a github repository URL.
