DEV Community

Cover image for A Beginner's Guide to Running a GoLang Project: Essential Steps
Nikhil Soman Sahu
Nikhil Soman Sahu

Posted on

A Beginner's Guide to Running a GoLang Project: Essential Steps

Your first project

**
Introduction**

This chapter will cover:

  1. **Creating a project.
  2. Organize your files.**

Module use cases

There are two interesting use cases with modules:

Consuming a module, you will use a combination of core modules and external 3rd party modules
Creating a module, in some cases you will create code that you or someone else will be able to use. For this scenario, you can create a module and upload it to GitHub.
Consume internal files
You want to split up your app in many different files. Let’s say you have the following files:

/app
  main.go
  /helper
    helper.go 
Enter fullscreen mode Exit fullscreen mode

What you are saying above is that your program consists of many files and that you want code in the fiile main.go to use code from helper.go for example.

To handle such a case, you need the following:

a project. By creating a project, you create a top-level reference that you can use in the import directive.
an import that points to the project root name as well as the path to the module you want to import.
You can use go mod init, this will initialize your project.

Creating a project
To create a project, you run go mod init and a name for a project, for example, “my-project”:

 go mod init my-project
Enter fullscreen mode Exit fullscreen mode

You end up with a go.mod file looking something like so:

 module my-project

   go 1.16
Enter fullscreen mode Exit fullscreen mode

The go.mod file tells you the name of your project and the currently used version of Go. It can contain other things as well like libraries you are dependent on.

The import statement
Imagine now we have this file structure in our project:

/app
  main.go
  /helper
    helper.go 
Enter fullscreen mode Exit fullscreen mode

with helper.go looking like so:

package helper

import "fmt"

func Help() {
  fmt.Println("This is a helper function")
}
Enter fullscreen mode Exit fullscreen mode

to use the public Helper() function from main.go, we need to import it.

In main.go we need an import statement like so:

import (
  "my-project/helper"
)
Enter fullscreen mode Exit fullscreen mode

We are now able to invoke the Help() function from main.go like so:

helper.Help()
Enter fullscreen mode Exit fullscreen mode

Assignment - create a project

In this assignment, you will create a project.

Create a project like so:

go mod init my-project
Enter fullscreen mode Exit fullscreen mode

create the helper directory and helper.go file and give it the following content:

// helper.go

 package helper

 import "fmt"

 func Help() {
  fmt.Println("This is a helper function")
 }
Enter fullscreen mode Exit fullscreen mode

Create the main.go file and give it the following content:

package main

import (
  "my-project/helper"
)

func main() {
  helper.Help()
}
Enter fullscreen mode Exit fullscreen mode

Note this import "my-project/helper", it ensures the helper package is in scope.

Compile and run

go run main.go
Enter fullscreen mode Exit fullscreen mode

Solution

helper/helper.go

package helper

import "fmt"

func Help() {
 fmt.Println("help")
}
Enter fullscreen mode Exit fullscreen mode

main.go

package main

import "my-project/helper"

func main() {
 helper.Help()
}
Enter fullscreen mode Exit fullscreen mode

Challenge
See if you can create another function in helper.go, this time, make the function name lowercase, what happens if you try to import it?
credit github.io

Top comments (0)