GoLang Tutorial - Modules 0 (Using External Go Modules from GitHub)
bogotobogo.com site search:
Using External Go Modules from GitHub
In this post, we'll reverse a string in our main package where we call String function. The function is in reverse package that we can download from go_tutorial.
To use an external module, we need to do the following:
- Import it:
import "github.com/einsteinish/go_tutorial/string_util"
- Call the code from the module:
reversedString := reverse.String("Hello, world!")
- Downloaded it: via either
go mod tidy
orgo get github.com/einsteinish/go_tutorial/string_util
Here is the code, hello_world.go:
package main import ( "fmt" "github.com/einsteinish/go_tutorial/string_util" ) func main() { reversedString := reverse.String("Hello, world!") fmt.Println(reversedString) // Output: !dlrow ,olleH }
$ go mod init hello go: creating new go.mod: module hello go: to add module requirements and sums: go mod tidy
go.mod:
module hello go 1.21.4
To import the module, we can do either one of following two:
go mod tidy
checks the imports used in our code and fetches the module if not fetched already.$ go mod tidy go: finding module for package github.com/einsteinish/go_tutorial/string_util go: found github.com/einsteinish/go_tutorial/string_util in github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f
We can see go.mod has been updated:
module hello go 1.21.4 require github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f
go get
will fetch the module and download it and make it available for our project to use.$ go get github.com/einsteinish/go_tutorial/string_util go: added github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f
We can see go.mod has been updated:
module hello go 1.21.4 require github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f // indirect
Regardless of our choice, we get an identical output:
$ go run hello_world.go !dlrow ,olleH
FYI:
Files we have created so far:
go.mod:
module hello go 1.21.4 require github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f // indirect
go.sum:
github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f h1:FOZzpCJzzJyK2hhYYVTWNpGSauFwvv+ZPVer+vHWmQQ= github.com/einsteinish/go_tutorial v0.0.0-20231115000907-59bccb90e58f/go.mod h1:vdljcg3Z2tZ56EhjeU/+nbjIWq8oV/ztSx2ZvXqgt8s=
hello_world.go:
package main import ( "fmt" "github.com/einsteinish/go_tutorial/string_util" ) func main() { reversedString := reverse.String("Hello, world!") fmt.Println(reversedString) // Output: !dlrow ,olleH }
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization