Golang is a programming language developed by Google. It can used to create cloud services, web , CLI tools etc. The first step to use Go is download and install the SDK.
Basic setup
To build basic API in Golang we need following packages
- Fiber
- log
- fmt
go get -u github.com/gofiber/fiber/v2
Fiber is express inspired library, if you were familiar with Nodejs express package, no worries . In the main.go file add the following
package main
import
("github.com/gofiber/fiber/v2"
"fmt"
"log"
)
func api(c *fiber.Ctx) error {
msg := fmt.Sprintf("✋ %s", c.Params("*"))
return c.SendString(msg) // => ✋ register
}
func index(c *fiber.Ctx) error {
return c.SendString("I am Fiber") // => ✋ register
}
func main(){
app:= fiber.New()
app.Get("/",index )
app.Get("/api/*",api )
log.Fatal(app.Listen(":3000"))
}
Here we create a Fiber router instance which can be used to create API route. We also define handler function for the API too.
:= is the short hand in Golang for creating variables
Run the project
By using go run . or go run main.go can execute the program.
- How to add packages in Golang
- Create REST API using gin in Golang
- How to Containerize Golang app with Docker
- How to create API using mux in Golang
- How to create API using gin in Golang
- How to create API using Fiber in Golang
- Set up golang project
- How to use GORM packages in Golang
- How to solve gcc error in Golang -Windows