Difference between revisions of "Golang/Basics"
< Golang
		
		
		
		Jump to navigation
		Jump to search
		| Line 12: | Line 12: | ||
| </source> | </source> | ||
| = Go modules = | = Go modules and custom packages = | ||
| A Go Module is  | '''A Go Module is a collection of Go packages'''. Why do we need Go modules to create a custom package? The answer is the import path for the custom package we create is derived from the name of the go module. In addition to this, all the other third-party packages(such as source code from github) which our application uses will be present in the <code>go.mod</code> file along with the version. This go.mod file is created when we create a new module. | ||
| <source lang=bash> | <source lang=bash> | ||
| # we are in mymodule/ directory | # we are in mymodule/ directory | ||
| Line 23: | Line 23: | ||
| </source> | </source> | ||
| The <code>mymodule</code> is the base path to import any package created inside this module. | The <code>mymodule</code> is the base path to import any package created inside this module. | ||
| Source files belonging to a package should be placed in separate folders of their own. It is a convention in Go to name this folder with the same name as the package. | |||
Revision as of 14:54, 29 August 2021
Main function
package main
import (
	"fmt"
	"time"
)
func main()         {
	fmt.Println("Welcome", time.Now())
}
Go modules and custom packages
A Go Module is a collection of Go packages. Why do we need Go modules to create a custom package? The answer is the import path for the custom package we create is derived from the name of the go module. In addition to this, all the other third-party packages(such as source code from github) which our application uses will be present in the go.mod file along with the version. This go.mod file is created when we create a new module.
# we are in mymodule/ directory go mod init mymodule # create 'go.mod' file # cat go.mod module mymodule go 1.17
The mymodule is the base path to import any package created inside this module.
Source files belonging to a package should be placed in separate folders of their own. It is a convention in Go to name this folder with the same name as the package.