Logo

dev-resources.site

for different kinds of informations.

The Go Playground: Using Modules

Published at
11/6/2023
Categories
go
playground
modules
Author
matthewdale
Categories
3 categories in total
go
open
playground
open
modules
open
Author
11 person written this
matthewdale
open
The Go Playground: Using Modules

This is part of an upcoming series about useful and surprising features of the Go Playground. Follow me to read more about the Go Playground!


The Go Playground is extremely useful for sharing runnable Go code and testing out standard library functions, but what if you want to test out code that's in an external library? It turns out that the Go Playground can automatically fetch Go modules when you import packages that aren't in the standard library.

Let's check out an example of generating a random UUIDv4 using the github.com/google/uuid package:

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    u, err := uuid.NewRandom()
    if err != nil {
        panic(err)
    }

    fmt.Println(u)
}
Enter fullscreen mode Exit fullscreen mode

Run

When we run that code, the Go playground fetches the github.com/google/uuid module, then compiles and runs our code. By default, the Go Playground fetches the latest available module version for the specified package. But what if we want to use a different version of the module?

Major Versions

To fetch a different major version, add a major version suffix to the import path and the Go Playground will fetch that major version instead! For example, to fetch package mypackage from version v2.x.x of the github.com/name/example module, import package

github.com/name/example/v2/mypackage
Enter fullscreen mode Exit fullscreen mode

Let's check out an example of writing a simple HTTP handler using v5 of the github.com/go-chi/chi module:

package main

import (
    "fmt"
    "io"
    "net"
    "net/http"
    "os"

    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hi"))
    })

    listener, err := net.Listen("tcp", ":8080")
    if err != nil {
        panic(err)
    }
    go http.Serve(listener, r)

    fmt.Println("Sending request...")
    res, err := http.Get("http://localhost:8080/")
    if err != nil {
        panic(err)
    }

    fmt.Println("Reading response...")
    if _, err := io.Copy(os.Stdout, res.Body); err != nil {
        panic(err)
    }
}
Enter fullscreen mode Exit fullscreen mode

Run

That example highlights another feature of the Go Playground: you can run and test local network services, like HTTP servers! Your code can't access the Internet, but you can run as many local services as the Go Playground sandbox can handle.

Any Version

So far we've let the Go Playground fetch the latest version and a specific major version, but we can also specify the exact version of a module if we want. To do that, we'll use another hidden feature of the Go Playground: you can add multiple source code files!

In this example, we'll add a go.mod file that specifically requests v1.11.7 of the go.mongodb.org/mongo-driver module:

package main

import (
    "fmt"

    "go.mongodb.org/mongo-driver/version"
)

func main() {
    fmt.Println(version.Driver)
}
-- go.mod --
module mymodule

require go.mongodb.org/mongo-driver v1.11.6
Enter fullscreen mode Exit fullscreen mode

Run

That's all for now! In the next Go Playground post, we'll look at displaying images and GIFs in the Go Playground. Subscribe for more!

modules Article's
30 articles in total
Favicon
Python Day-9 Predefined modules,While loop,Task
Favicon
Understanding JavaScript Modules: Exporting and Importing Code Made Easy
Favicon
Python Day - 8 Modules-Meaning and Types,Tasks
Favicon
Day 8 - Modules
Favicon
Node Express
Favicon
Terraform Modules Best Practices for Better Cloud Management
Favicon
Exploring Medusa 2.0 Commerce Modules
Favicon
Advanced Terraform Module Usage: Versioning, Nesting, and Reuse Across Environments
Favicon
Modules 1/2
Favicon
Collaborate and Stay Ahead with NEW Forge Updates
Favicon
Mind the Terraform Modules
Favicon
The Ongoing War Between CJS & ESM: A Tale of Two Module Systems
Favicon
Top 10 AI Edge Engineer Modules by Microsoft
Favicon
Meet the new Puppet Forge
Favicon
How to reduce the cost of LED display modules
Favicon
Terraform - Modules & Demos
Favicon
Types of Node Modules
Favicon
Unraveling the Monolith: Terraform Configuration Refactoring in Depth
Favicon
πŸ“Œ How can you use Terraform modules?
Favicon
EcmaScript Modules na prΓ‘tica
Favicon
The Go Playground: Using Modules
Favicon
What is the general resolution of LED display modules?
Favicon
What are the main components of an LED display?
Favicon
Python: Imports and modules
Favicon
Golang Environment: GOPATH vs go.mod
Favicon
Javascript Modules
Favicon
Install my development environment In Prestashop
Favicon
Reversing a Linked List: A Hallway Analogy
Favicon
Everything You Need To Know About Node.js Modules
Favicon
Content & Tooling Team Status Update

Featured ones: