How to create a package in golang.
Create your own custom go packages for re-usability.

Search for a command to run...
Create your own custom go packages for re-usability.

https://gist.github.com/fuadop/88aaf88f07771c88de10a3eeb70edff1
https://gist.github.com/fuadop/90f67285e6f250c76aafd698ba764331
/** * @param {string} b * @returns {string} */ const btoa = (b) => { let s = ''; const bytes = new Uint8Array(b.split('').map(x => x.charCodeAt(0))); const ALPHABET_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567...
tmux -V &> /dev/null if [[ $? -eq 0 ]]; then function clear_tmux_scrollback_buffer() { tmux clear-history } zle -N clear_tmux_scrollback_buffer bindkey "^[^L" clear_tmux_scrollback_buffer fi https://github.com/fuadop/zsh-conf...
A Comprehensive Guide to Object-Oriented Programming (OOP) in Golang

In golang we use a package using the import keyword.
For a single package:
import "fmt"
For multiple packages, we use the multi line syntax:
import (
"fmt"
"math"
"log"
)
Similar to initializing multiple variables:
var (
a int
b int
c int
)
package main at the top of every go file means?It's a package declaration, all go files must belong to a package.
The go run command can only execute go files in the main package.
Now that we know about packages lets move to creating our own package, we would be creating a furthermaths package.
Don't worry your furthermaths package would be easier than regular mathematics 🙂.
e.g
mkdir $GOPATH/src/github.com/fuadop/furthermaths
cd $GOPATH/src/github.com/fuadop/furthermaths
go mod init
You can name it whatever you wish. I would go with math.go.
touch math.go
Instead of adding package main to the top of the file, we would be adding our package name.
i.e
package furthermaths
We would be defining only one function in our package, which is the Add function.
Before we continue, there is something you must know when defining functions in a custom package.
This is similar to private and public members of a class in OOP (Object Oriented Programming).
Now that you know that, let us go on with defining our Add function.
package furthermaths
// Adds multiple amount of numbers
func Add(nums ...int) int {
var result int
for _, num := range nums {
result += num
}
return result
}
We have finally created a furthermaths package with an Add function.
To use this function in another project, you need to install the package using the go get command.
go get github.com/fuadop/furthermaths
The function can be used like so:
package main
import (
"fmt"
"github.com/fuadop/furthermaths"
)
func main() {
fmt.Println(furthermaths.Add(1, 2, 3, 4))
}
Running the above command would print 10 to the console.
10
To get the package source code, click here.