From the command line in the directory containing main.go, run the code to start the server.

$ go run .
The command should display JSON for the album whose ID you used. If the album wasn’t found, you’ll get JSON with an error message.

{
        "id": "2",
        "title": "Jeru",
        "artist": "Gerry Mulligan",
        "price": 17.99
}
Finally, change your main so that it includes a new call to router.GET, where the path is now /albums/:id, as shown in the following example.

func main() {
    router := gin.Default()
    router.GET("/albums", getAlbums)
    router.GET("/albums/:id", getAlbumByID)
    router.POST("/albums", postAlbums)

    router.Run("localhost:8080")
}

Recommend

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return a specific item Write the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to add a new item Run the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to add a new item Write the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return all items Run the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return all items Write the code

Tutorial: Developing a RESTful API with Go and Gin Create the data Write the code

Tutorial: Developing a RESTful API with Go and Gin Create a folder for your code

Go Tutorial: Accessing a relational database Completed code

Go Tutorial: Accessing a relational database Add data Write the code

Go Tutorial: Accessing a relational database Query for a single row Write the code

Go Tutorial: Accessing a relational database Query for multiple rows Run the code

Go Tutorial: Accessing a relational database Query for multiple rows Write the code

Go Tutorial: Accessing a relational database Get a database handle and connect Run the code

Go Tutorial: Accessing a relational database Get a database handle and connect Write the code

Go Tutorial: Accessing a relational database Find and import a database driver

Go Tutorial: Accessing a relational database Set up a database

Go Tutorial: Accessing a relational database Create a folder for your code

Go Tutorial: Getting started with generics Completed code

Go Tutorial: Getting started with generics Declare a type constraint Write the code

Go Tutorial: Getting started with generics Add a generic function to handle multiple types Write the code

Go Tutorial: Getting started with generics Add non-generic functions Run the code

Go Tutorial: Getting started with generics Add non-generic functions Write the code

Go Tutorial: Getting started with generics Create a folder for your code

Go Setting up and using gccgo C Interoperability Function names

Go Setting up and using gccgo C Interoperability Types

Go Setting up and using gccgo Imports

Go Setting up and using gccgo Using gccgo

Go Setting up and using gccgo Building Build commands

Setting up and using gccgo Building Gold

Installing Go from source Optional environment variables

Installing Go from source Keeping up with releases

Installing Go from source Install additional tools

Installing Go from source Testing your installation

Installing Go from source Install Go

Installing Go from source (Optional) Switch to the master branch

Installing Go from source Fetch the repository

Installing Go from source Install Go compiler binaries for bootstrap Bootstrap toolchain using gccgo

Installing Go from source Install Go compiler binaries for bootstrap Bootstrap toolchain from cross-compiled source

Go Managing dependencies Specifying a module proxy server

Go Managing dependencies Removing a dependency

Go Managing dependencies Getting a specific commit using a repository identifier

Go Managing dependencies Developing and testing against unpublished module code Requiring external module code from your own repository fork

Go Managing dependencies Developing and testing against unpublished module code Requiring module code in a local directory

Go Managing dependencies Synchronizing your code’s dependencies

Go Managing dependencies Discovering available updates

Go Managing dependencies Getting a specific dependency version