Part 1 - Nuxnuxx/showcase_go GitHub Wiki
Scaffolding
go mod init github.com/user/project
After that create a main.go
and add it.
// Filename: main.go
package main
func main(){
}
This is the entrypoint of your binary.
Setting up
Intall echo
go get github.com/labstack/echo/v4
Get your Api Key for RAWG
What is RAWG : Here
Now put it in your .env.
//Filename: .env
API_KEY="yourapikey"
Load .env
This package his self explanatory, it just autoload what is in the .env
automatically for us.
go get "github.com/joho/godotenv"
// Filename: main.go
import (
...
_ "github.com/joho/godotenv/autoload"
)
Get started with RAWG
// Filename: main.go
// GET API_KEY from .env file
API_KEY := os.Getenv("API_KEY")
// Build the URL
builder := strings.Builder{}
builder.WriteString("https://api.rawg.io/api/games?key=")
builder.WriteString(API_KEY)
// Make the request
resp, err := http.Get(builder.String())
if err != nil {
panic(err)
}
// Defer the closing of the response body
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Print the response body
fmt.Println(string(body))
// Now the body is close automatically because there is no need for it anymore
Lets run this
// Filename: Makefile
build:
@go build -o bin/app
run: build
@./bin/app
// Nobody write test
test:
@go test -v ./...
make run
it will build and then start the binary.
to see a more clear response you select only the name of the games with jq
.
make run | jq -r '.results[].name'
| Checkpoint |
git reset --hard HEAD
git merge origin/get_started_with_rawg