Question

How to Install a Package with go get in Go

gogo-toolchain

Question

I want to install a package from GitHub into my $GOPATH using go get, but this command does not work:

go get github.com:capotej/groupcache-db-experiment.git

The repository is hosted on GitHub. What is the correct way to install or download this package with go get?

Short Answer

By the end of this page, you will understand how go get identifies Go packages, why GitHub repository paths must use Go import path format instead of Git clone syntax, and how to correctly fetch packages from GitHub in Go projects.

Concept

In Go, go get works with package import paths, not raw Git repository URLs.

That means Go expects a path like this:

github.com/user/repository

not this:

github.com:user/repository.git

The second format looks like a Git SSH-style repository address, which is valid for git clone, but not for go get.

Why this matters

Go tools are designed around import paths. These paths tell the Go toolchain:

  • where the code lives
  • how to download it
  • how to refer to it in your source code

For example, if your code imports:

import "github.com/capotej/groupcache-db-experiment"

then the matching go get command is:

go get github.com/capotej/groupcache-db-experiment

Important context

Historically, go get was commonly used to download code into $GOPATH. In modern Go, projects usually use Go modules, and dependencies are stored in the module cache instead of being manually managed in $GOPATH.

So the underlying idea is still the same:

  • use the package's import path
  • do not use .git
  • do not use : after github.com

If you are learning from older examples, you will often see $GOPATH mentioned. That was the standard workflow before Go modules became the default.

Mental Model

Think of go get like asking Go for a book by its library catalog name, not by the warehouse shipping address.

  • Import path = the catalog name Go understands
  • Git URL = the shipping or transport address used by Git

If you ask the Go tool using the wrong naming system, it cannot find the package correctly.

So:

  • github.com/capotej/groupcache-db-experiment ✅
  • github.com:capotej/groupcache-db-experiment.git ❌

Go wants the package name in its own ecosystem's format.

Take Quiz

Syntax and Examples

The basic syntax is:

go get package/import/path

Correct example

For the repository in your question, use:

go get github.com/capotej/groupcache-db-experiment

Why this works

This is the correct Go import path format:

  • domain name first
  • then username or organization
  • then repository name

Example in code

If a package exposes code you want to use, your import might look like this:

package main

import (
    "fmt"
    _ "github.com/capotej/groupcache-db-experiment"
)

func main() {
    fmt.Println("package fetched successfully")
}

The blank identifier _ import means the package is imported only for side effects. In normal code, you would usually import and use exported functions or types.

Modern module-based workflow

Inside a Go project, you would typically run:

go mod init example.com/myapp
go get github.com/capotej/groupcache-db-experiment

This adds the dependency to your module setup rather than manually thinking about $GOPATH.

Step by Step Execution

Consider this command:

go get github.com/capotej/groupcache-db-experiment

Here is what happens conceptually:

1. Go reads the import path

Go sees:

  • host: github.com
  • owner: capotej
  • repository: groupcache-db-experiment

2. Go resolves the repository

The Go tool determines where the source code is hosted and how to fetch it.

3. Go downloads the code

Depending on your Go setup:

  • older GOPATH mode: code is placed under $GOPATH/src/...
  • module mode: code is stored in the module cache

4. Go records dependency information

In module mode, go.mod and possibly go.sum are updated.

5. Your code can import the package

You can then use the same import path in Go source files:

import "github.com/capotej/groupcache-db-experiment"

Wrong command trace

If you run:

go get github.com:capotej/groupcache-db-experiment.git

Go treats this as an invalid package path because:

  • : is not the expected separator here
  • .git is not part of the normal Go import path
  • the format matches Git transport syntax, not Go package syntax

Real World Use Cases

Using go get or Go dependency fetching is common in many practical situations:

Adding a library to an API server

You may want to use a router, logger, or database driver:

go get github.com/gin-gonic/gin

Installing a CLI dependency in a project

A project may need a package for configuration, validation, or parsing:

go get github.com/spf13/viper

Pulling code used by internal tooling

Build scripts, deployment tools, or data migration tools often depend on external packages.

Downloading code from source control consistently

Using import paths gives teams a stable, language-native way to reference dependencies across machines and environments.

Take Quiz

Real Codebase Usage

In real Go projects, developers usually do not think in terms of manually filling $GOPATH. Instead, they work with modules and let Go manage dependencies.

Common patterns

Add a dependency from inside a module

go mod init example.com/myapp
go get github.com/sirupsen/logrus

Import what you use

import "github.com/sirupsen/logrus"

Keep dependency definitions in versioned files

Real projects commit:

  • go.mod
  • go.sum

This makes builds reproducible.

Use package paths consistently

The path used in go get should match the path used in import.

Related development patterns

  • Validation: ensure the package path is correct before assuming installation failed
  • Configuration: module mode is controlled by project structure and Go environment
  • Error handling: if fetching fails, developers check path spelling, repository visibility, and module compatibility
  • Early diagnosis: first verify whether the package path is a Go import path or only a Git clone URL

Common Mistakes

1. Using a Git URL instead of a Go import path

Broken:

go get github.com:capotej/groupcache-db-experiment.git

Correct:

go get github.com/capotej/groupcache-db-experiment

2. Adding .git unnecessarily

Broken:

go get github.com/user/project.git

Correct:

go get github.com/user/project

Go usually wants the import path, not the repository suffix.

3. Confusing GOPATH mode with module mode

Beginners often expect all downloaded code to appear directly inside $GOPATH/src. In modern Go, dependencies are usually handled through modules.

4. Using the wrong import path in code

If you fetched:

go get github.com/capotej/groupcache-db-experiment

then your import should match that path exactly.

5. Assuming every GitHub repository is a usable Go package

A repository may:

  • contain multiple packages
  • be a command, not a library
  • require a specific subpackage path
  • be outdated or incompatible

Always check the repository's Go package structure.

6. Ignoring private repository access

If the repository is private, authentication and Go environment setup may be required. A correct path alone is not enough.

Comparisons

ConceptExampleUsed ByCorrect for go get?
Go import pathgithub.com/capotej/groupcache-db-experimentGo toolchainYes
Git SSH-style URLgithub.com:capotej/groupcache-db-experiment.gitgit clone in some contextsNo
HTTPS Git URLhttps://github.com/capotej/groupcache-db-experiment.gitGitUsually not the normal go get form

GOPATH mode vs module mode

ModeDependency locationCommon today?Notes
GOPATH mode$GOPATH/srcLess commonOlder workflow
Module modeModule cache + go.modYesStandard modern workflow

go get vs git clone

ToolPurposeInput format
go getFetch Go dependencies using import pathsgithub.com/user/repo
git cloneClone repositories using Git transport URLsSSH or HTTPS Git URL
Take Quiz

Cheat Sheet

go get github.com/user/repo

Rules

  • Use the Go import path
  • Do not use : after github.com
  • Do not add .git unless documentation specifically requires something unusual
  • The path used in go get should usually match the path used in import

Correct

go get github.com/capotej/groupcache-db-experiment

Incorrect

go get github.com:capotej/groupcache-db-experiment.git

Remember

  • go get is for Go package paths
  • git clone is for Git repository URLs
  • modern Go usually uses modules instead of manually managing $GOPATH

Quick check list

  • Is the host correct?
  • Is the username or org correct?
  • Is the repository name correct?
  • Did you remove .git?
  • Did you replace : with /?
  • Are you inside a Go module if using modern Go?

FAQ

Why does go get github.com:... fail?

Because that is Git-style syntax, not a Go import path. go get expects github.com/user/repo.

Should I include .git in a go get command?

Usually no. Go package paths normally do not include .git.

Does go get install code into $GOPATH?

In older GOPATH workflows, yes. In modern Go, dependencies are usually stored in the module cache and tracked in go.mod.

What is the correct command for this repository?

go get github.com/capotej/groupcache-db-experiment

Can I use git clone instead of go get?

Yes, but that solves a different problem. git clone copies the repository, while go get works with Go dependency management.

Why must the import path and fetch path match?

Because Go uses the import path as the package identity. Consistent paths help the toolchain locate and build dependencies correctly.

Take Quiz

Mini Project

Description

Build a tiny Go program that imports an external package using the correct Go import path format. This project helps reinforce the difference between a Go package path and a Git repository URL.

Goal

Create a Go module, add an external dependency with go get, and run a program that uses it successfully.

Requirements

  1. Create a new Go project folder.
  2. Initialize the project as a Go module.
  3. Add an external package using go get with the correct import path format.
  4. Write a small program that imports and uses the package.
  5. Run the program successfully.
Take Quiz

Keep learning

Related questions

View more