| name | encore-go-getting-started |
| description | Get started with Encore Go. |
Getting Started with Encore Go
Instructions
Install Encore CLI
brew install encoredev/tap/encore
curl -L https://encore.dev/install.sh | bash
iwr https://encore.dev/install.ps1 | iex
Create a New App
encore app create my-app
encore app create my-app --example=hello-world
Project Structure
A minimal Encore Go app:
my-app/
├── encore.app # App configuration
├── go.mod # Go module
└── hello/ # A service (package with API)
└── hello.go # API endpoints
The encore.app File
// encore.app
{
"id": "my-app"
}
This file marks the root of your Encore app. The id is your app's unique identifier.
Create Your First API
In Encore Go, any package with an //encore:api endpoint becomes a service:
package hello
import "context"
type Response struct {
Message string `json:"message"`
}
func Hello(ctx context.Context) (*Response, error) {
return &Response{Message: "Hello, World!"}, nil
}
Run Your App
encore run
Open the Local Dashboard
encore run
The dashboard shows:
- All your services and endpoints
- Request/response logs
- Database queries
- Traces and spans
Common CLI Commands
| Command | Description |
|---|
encore run | Start the local development server |
encore test | Run tests (uses go test under the hood) |
encore db shell <db> | Open a psql shell to a database |
encore gen client | Generate API client code |
encore app link | Link to an existing Encore Cloud app |
Add Path Parameters
type GetUserParams struct {
ID string
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
return &User{ID: params.ID, Name: "John"}, nil
}
Add a Database
package hello
import "encore.dev/storage/sqldb"
var db = sqldb.NewDatabase("mydb", sqldb.DatabaseConfig{
Migrations: "./migrations",
})
Create a migration:
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
Query the Database
import "encore.dev/storage/sqldb"
type Item struct {
ID int
Name string
}
func getItem(ctx context.Context, id int) (*Item, error) {
item, err := sqldb.QueryRow[Item](ctx, db, `
SELECT id, name FROM items WHERE id = $1
`, id)
if err != nil {
return nil, err
}
return item, nil
}
Next Steps
- Add more endpoints (see
encore-go-api skill)
- Add authentication (see
encore-go-auth skill)
- Add infrastructure like Pub/Sub, cron jobs (see
encore-go-infrastructure skill)
- Deploy to Encore Cloud:
encore app link then git push encore