---
# System prepended metadata

title: Set up Golang Project
tags: [golang]

---

# Set up Golang Project
###### tags: `golang`

[TOC]
# Module Creation
## What is Go Module?
Module in Go is the unit of distribution and versioning of software.
## How to create?
```go=
go mod init cztutorial.com/go-demo
// "go-demo" is the name of the module
// "cztutorial.com" is the location in internet to download/ Github account
```
![](https://i.imgur.com/ekijyWX.png)

# Go Package
## What is Go Package?
It is a folder that contains Go code

## How to create one?
Just open up a folder and put go files inside it.

## **package** XXX
Use **package name** or **package name_test**, depending on whether that file inside the package is a test file or not.

# Go Function
## Go Function Naming Rule
Go uses MixedCaps or mixedCaps rather than underscores to write multiword names.

It is important to take time to name functions in Go language, because whether it is **MixedCaps or mixedCaps** will affect the visibility of the name outside or inside a package.

**Note:**
> writeToMongoDB // unexported, only visible within the package

> WriteToMongoDB // exported

## How to access function outside package?
First of all, make sure the function name is written in **MixedCaps** style. 

Second, you have to use package name in order to access the function. In this case **mascot** package is the one used to access **BestMascot** function. By default, Go will do the import process.

![](https://i.imgur.com/iiaaqiB.png)

## How to access function from a package in Internet?
First of all, you need to require it in your **module file (.mod)**, and after that when using the function, the go file will automatically import it.

>![](https://i.imgur.com/2dT1zfz.png)[color=#ccccff]


# Run Go File
**Note:** your main go file must be **package main**
```go=
// go run fileName.go
go run main.go
```
![](https://i.imgur.com/3lVqPCj.png)


# Log in Go
In Go, we use Format package to make logs. By typing **fmt** we can access to its functions and record a log.