GoH - Go Helpers package to help with golang code creation productivity
This project was created for study purposes and use in personal projects, feel free to give me any feedback about the project, improvements, add new features, structure or anything you can report that can improve the project and spread knowledge!
Functionalities:
- (core) Some useful functions for shared use, convert, encoder, types etc.
- (database) singleton's for database connection management and cache, SQL DML generator to facilitate database creation and manipulation
- (environment) Helpers for reading and configuring environment variables (.env) we can have different configurations for each environment (production, development).
- (httprest) Helpers for creating and configuring the Server, Routes, Database initialization and caching...
- (httpwr) Helpers to help with handling and handling Request and Response, Json, Errors, Body Reading, Params and Querys...
Get started
Installation
go get github.com/joaocprofile/goh
Create the .env file in your project ./
Examples:
Creation of Controllers
// user_controller.go
package users
import (
"net/http"
"strconv"
"github.com/joaocprofile/goh/httpwr"
)
type User struct {
Id uint `json:"id"`
Name string `json:"name"`
}
var users = []User{
{Id: 1, Name: "John doe"},
{Id: 2, Name: "Big john"},
}
func Get(w http.ResponseWriter, r *http.Request) {
sid, err := httpwr.Params("id", w, r)
if err != nil {
return
}
id, _ := strconv.Atoi(sid)
id -= 1
user := users[id]
httpwr.Response(w, http.StatusOK, user)
}
func GetAll(w http.ResponseWriter, r *http.Request) {
httpwr.Response(w, http.StatusOK, users)
}
Creation of routes
// users_routes.go
import (
"net/http"
"github.com/joaocprofile/goh/core"
)
var usersRoutes = []core.Route{
{
URI: "/users/{id}",
Method: http.MethodGet,
Function: Get,
Authentication: false,
},
{
URI: "/users",
Method: http.MethodGet,
Function: GetAll,
Authentication: false,
},
}
// main.go
// Simple HTTP server
import (
"net/http"
"github.com/joaocprofile/goh/environment"
"github.com/joaocprofile/goh/httprest"
)
func main() {
environment.Inicialize()
apiRest := httprest.NewHttpRest()
apiRest.AddRoutes(usersRoutes)
apiRest.ListenAndServe()
}
✨ Used packages
The following packages were directly used in this project::
License
This project is licensed under the MIT License - see the LICENSE.md file for details