Go — 5

Himashi Karunathilake
3 min readJul 18, 2023
Link to Part 1: https://himashikarunathilake.medium.com/go-1-74940ce2556d
Link to Part 2: https://himashikarunathilake.medium.com/go-2-79dc2c04db26
Link to Part 3: https://himashikarunathilake.medium.com/go-3-acbfda8360ed
Link to Part 4: https://himashikarunathilake.medium.com/go-4-7b1ead499bb9

Given below is the main.go file that will be used to run all the sub files in this section:

// The fifth program in Go.

package main

import (
"fmt"
"packs"
)

func main() {
fmt.Println()

fmt.Println("*************** RUNNING THE GREETINGS.GO FILE ***************")
packs.SayHello()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE ANONYMOUS_FUNCTIONS.GO FILE ***************")
AnonymousFunctions()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()
}

Modules

Modules in Go provide a way to manage dependencies and versioning in your projects and allow you to define and maintain a specific set of packages and their versions that your project depends on.

Initializing a Module

To initialize a module, run the following command in a command prompt.

go mod init github.com/your_username/you_file_name
go.mod file created after initializing a module

Adding Dependencies

To add a dependency to a project, the required packages need to be imported. For example, supposing you need to include a MySQL driver for your Go project, then you can import the following package in your project:

import "github.com/go-sql-driver/mysql"

Managing Dependencies

Dependencies and their versions can be managed through the go.mod file that is created.

Updating Dependencies

To update all the dependencies in your project, run the following command in a terminal:

go get -u

Packages

Go packages can be used to group related functions, types, and variables together.

Package Declaration

Each Go source file starts with a package declaration that specifies the package to which the file belongs.

The package name should be lowercase.

package package_name

Exported and Un-exported Names

Name starts with a:

Uppercase Letter Exported Accessible from other packages

Lowercase Letter Un-exported Can only be accessed within the same
package
package packs

import "fmt"

// Exported function
func SayHello() {
fmt.Println("Hello!")
}

// Un-exported function
func sayGoodbye() {
fmt.Println("Goodbye!")
}

Importing Packages

To use functions, types, or variables from another package, it needs to be imported.

// The fifth program in Go.

package main

import (
"fmt"
"packs"
)

func main() {
fmt.Println()

fmt.Println("*************** RUNNING THE GREETINGS.GO FILE ***************")
packs.SayHello()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()
}
Importing a function from another package

Anonymous Functions

Anonymous functions are defined in line and do not have a name. They are often used as callbacks or as the body of a for loop.

Assigning an Anonymous Function to a Variable

package main

import "fmt"

func AnonymousFunctions() {
// Assign an anonymous function to a variable
sum := func(num1, num2 int) int {
return num1 + num2
}
}

Calling an Anonymous Function

package main

import "fmt"

func AnonymousFunctions() {
// Assign an anonymous function to a variable
sum := func(num1, num2 int) int {
return num1 + num2
}

// Call the anonymous function
var num1, num2 int

fmt.Print("Please provide the first number: ")
fmt.Scan(&num1)
fmt.Print("Please provide the second number: ")
fmt.Scan(&num2)

fmt.Println()

fmt.Printf("The addition of the two numbers are: %d\n", sum(num1, num2))
}
Calling an anonymous function

--

--

Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!