Golang Cheatsheet
Go Project Structure
project-name/
├── cmd/
│ └── myapp/
│ └── main.go
├── internal/
│ └── domain/
│ └── model.go
│ └── usecase/
│ └── interactor.go
│ └── interface/
│ └── repository.go
│ └── infrastructure/
│ └── repository_impl.go
├── pkg/
│ └── utility/
│ └── util.go
├── api/
│ └── api.go
├── configs/
│ └── config.yaml
├── scripts/
│ └── setup.sh
└── README.md
Details
- cmd/: Contains the main application entry points.
- internal/: Contains domain logic, use cases, interfaces, and infrastructure implementations.
- domain/: Contains business models and entities.
- usecase/: Contains application use cases and interactors.
- interface/: Contains interface definitions for repositories and external services.
- infrastructure/: Contains infrastructure-specific implementations like database access.
- pkg/: Contains shared utility code.
- api/: Contains API-related code.
- configs/: Contains configuration files.
- scripts/: Contains scripts for setup and maintenance.
- README.md: Provides an overview of the project.
Simple Testing Samples
package main
import "github.com/gin-gonic/gin"
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
return r
}
func main() {
r := setupRouter()
r.Run(":8080")
}
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestPingRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}
Written on October 10, 2024