How I Start New Go Projects in Seconds
Tags: go, terminal, cli
Published: 2020-03-09 08:56:12

---
title: How I Start New Go Projects in Seconds
published: true
date: 2020-03-09 00:00:00 UTC
tags: go,golang,terminal,cli
canonical_url: https://jagascript.com/how-i-start-new-go-projects-in-10-seconds/
---

* * *

For the past years, I’ve started using more and more golang as a scripting language, and since in the past we were forced to put code in the GOPATH it became a little cumbersome compared to just write a bash file in the current folder

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#project-goinit)Project goinit

![goinit](https://jagascript.com/8d81fbd6c936747da1a506d27fa98f99/goinit.gif)

So I created a go program called `goinit` to generate a new project in my `GOPATH` at `~/go/src/github.com/kandros/[project-name]` and open it in my editor, so far it server me very well, every new experiment is just a couple keystroke away `goinit my-app`, I literally can see something in the console in less than 15 seconds

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#using-go-mod)Using go mod

Since go 1.11 I’ve started using go modules and stopped using `goinit`, a couple days ago I decided to refactor it to handle creating projects outside `GOPATH` and decided to use an even simpler solution, a custom shell function

```
goinit() {
 mkdir -p ~/coding/$1
 cd ~/coding/$1
 cat <<-EOF > main.go
 package main

 import "fmt"

 func main() {
    fmt.Println("hello")
    }
 EOF

 go mod init github.com/kandros/$1
 code-insiders --new-window . --goto main.go
}
```

It works the same way as my previous script by running `goinit new-project` in the terminal it will create the folder `~/coding/new-project` open the project in VScode and the file main.go

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#make-the-function-accessible-globally)Make the function accessible globally

This function is stored in my `~/.zshrc` file to be available in the terminal, Bash users can put it in their `~/.bash_profile`