🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 05 : 59 : 11
1×
Become a member
to unlock all features
Autoplay

    Initializing a project using Go modules

    Chris BiscardiChris Biscardi
    goGo

    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
    Code

    Code

    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
    Discuss

    Discuss

    Transcript

    Transcript

    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.