Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Initializing a project using Go modules

    Chris BiscardiChris Biscardi

    Golang includes the go mod command to initialize a new project. Sometimes that command fails even though it tries to be smart about where to get the name of your module from so we give an example of how to explicitly set the name of your go modules based project.

    shellmkdir my-go-project
    cd my-go-project
    go mod init github.com/christopherbiscardi/my-go-project


    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: To initialize a new golang project with Go modules, first we'll create a new directory for our new project.

    Now that we're in our new project directory, we can run go mod init. Go mod init tries to be smart about how it creates your new Go modules project. You can see here that we are outside our GOPATH and we don't have any import comments in our project, because we don't even have any Go files yet.

    To tell Go modules what the name of our module is, we use go mod init, with the fully qualified path to our module. We can see that go mod has now succeeded. Also note that we have a new file, called go.mod, that includes our module and the Go version we used. When we add imports to our Go code later, they'll also be added to this file.