Skip to content

Instalasi

Sebelum memulai, pastikan kamu sudah menginstall:

  • Go 1.20 atau yang lebih baru - Download Go
  • Text editor - VS Code dengan Go extension sangat direkomendasikan
  • Terminal - Command prompt, PowerShell, atau terminal favorit kamu

Untuk mengecek versi Go:

Terminal window
go version
# Output: go version go1.21.0 darwin/amd64 (atau sejenisnya)
Section titled “Opsi 1: Pakai Starter Project (Recommended)”

Cara tercepat untuk memulai project baru:

Terminal window
# Clone repository
git clone https://github.com/semutdev/goigniter
cd goigniter
# Copy starter ke folder project baru
cp -r starter/ ~/projects/myapp
cd ~/projects/myapp
# Setup go.mod
mv go.mod.example go.mod
# Edit go.mod: ganti "myapp" dengan nama module kamu
# Install dependencies
go mod tidy
# Jalankan
go run main.go

Buka browser ke http://localhost:8080/welcomecontroller dan kamu akan melihat halaman welcome.

GoIgniter menyediakan beberapa contoh di folder examples/:

Terminal window
git clone https://github.com/semutdev/goigniter
cd goigniter
# Contoh sederhana
go run examples/simple/main.go
# Contoh dengan auto-routing
go run examples/autoroute/main.go
# Contoh lengkap dengan database
cd examples/full-crud
go mod tidy
go run main.go

Buka browser ke http://localhost:8080 dan kamu akan melihat hasilnya.

Jika kamu familiar dengan CI3, struktur folder GoIgniter akan terasa seperti rumah:

CodeIgniter 3: GoIgniter:
─────────────── ──────────
application/ application/
├── controllers/ ├── controllers/
├── models/ ├── models/
├── views/ ├── views/
├── config/ ├── config/
└── libraries/ └── libs/
system/ system/
├── core/
├── middleware/
├── libraries/
└── helpers/
index.php main.go

Perbedaan utama:

  • index.php diganti main.go sebagai entry point
  • Folder system/ berisi framework core (mirip CI3, tapi jangan dimodifikasi)
  • Tidak ada folder cache/ atau logs/ - Go handle ini secara berbeda

Berikut contoh main.go paling sederhana:

package main
import (
"goigniter/system/core"
"goigniter/system/middleware"
)
func main() {
// Buat aplikasi baru
app := core.New()
// Pasang middleware
app.Use(middleware.Logger())
app.Use(middleware.Recovery())
// Route sederhana
app.GET("/", func(c *core.Context) error {
return c.JSON(200, core.Map{
"message": "Hello GoIgniter!",
})
})
// Jalankan server
app.Run(":8080")
}

Jalankan dengan:

Terminal window
go run main.go

Buka http://localhost:8080 di browser, kamu akan melihat response JSON:

{"message": "Hello GoIgniter!"}

Secara default, kamu harus restart server setiap kali ada perubahan kode. Untuk development yang lebih nyaman, gunakan Air:

Terminal window
# Install air
go install github.com/cosmtrek/air@latest
# Jalankan dengan hot reload
air

Sekarang setiap kali kamu save file .go, server akan otomatis restart.


Sudah berhasil running? Lanjut ke Routing untuk belajar cara mendefinisikan routes.