A sample Go project demonstrating connection configuration using the functional options pattern.
This project demonstrates a flexible and readable configuration system using the functional options pattern in Go. The project uses self-referential function types to configure database connection parameters including server, port, database name, username, and password.
- Functional options pattern implementation
- Flexible configuration system
- Default values support
- Connection string generation
- Multiple configuration options (server, port, database, username, password)
Clone the repository:
git clone <repository-url>
cd functional-options-goTo run the project:
go run main.go// With all default values
conn := NewConnection()
// With custom port, database, username, and password
port := WithPort(8081)
database := WithDatabase("app")
username := WithUsername("user")
password := WithPassword("secret")
conn := NewConnection(port, database, username, password)
// With custom server
server := WithServer("192.168.1.100")
conn := NewConnection(server, WithPort(3306))
// Mix and match - only override what you need
conn := NewConnection(WithDatabase("production"), WithPort(5432))functional-options-go/
├── main.go # Main application code
├── go.mod # Go module definitions
└── README.md # This file
- Server:
localhost - Port:
9715 - Database:
test - Username:
root - Password:
root
WithServer(host string) Option: Sets the server addressWithPort(port int) Option: Sets the port numberWithDatabase(database string) Option: Sets the database nameWithUsername(username string) Option: Sets the usernameWithPassword(password string) Option: Sets the passwordNewConnection(...Option) *Connection: Creates a new connection with the provided options
source://user:Extremely_SecurE_p@ssMorD@app:8081
This project is open source and freely available for use.
Contributions are welcome! Please test your changes before submitting a pull request.
Go dilinde functional options pattern kullanarak bağlantı yapılandırması yapan bir örnek proje.
Bu proje, Go'da functional options pattern kullanarak esnek ve okunabilir bir yapılandırma sistemi gösterir. Proje, veritabanı bağlantı parametrelerini (sunucu, port, veritabanı adı, kullanıcı adı ve şifre) yapılandırmak için self-referential function types kullanır.
- Functional options pattern implementasyonu
- Esnek yapılandırma sistemi
- Varsayılan değerler desteği
- Bağlantı string'i oluşturma
- Birden fazla yapılandırma seçeneği (sunucu, port, veritabanı, kullanıcı adı, şifre)
Projeyi klonlayın:
git clone <repository-url>
cd functional-options-goProjeyi çalıştırmak için:
go run main.go// Tüm varsayılan değerlerle
conn := NewConnection()
// Özel port, veritabanı, kullanıcı adı ve şifre ile
port := WithPort(8081)
database := WithDatabase("app")
username := WithUsername("user")
password := WithPassword("secret")
conn := NewConnection(port, database, username, password)
// Özel sunucu ile
server := WithServer("192.168.1.100")
conn := NewConnection(server, WithPort(3306))
// İstediğiniz seçenekleri karıştırın - sadece değiştirmek istediğiniz değerleri belirtin
conn := NewConnection(WithDatabase("production"), WithPort(5432))functional-options-go/
├── main.go # Ana uygulama kodu
├── go.mod # Go modül tanımları
└── README.md # Bu dosya
- Server:
localhost - Port:
9715 - Database:
test - Username:
root - Password:
root
WithServer(host string) Option: Sunucu adresini ayarlarWithPort(port int) Option: Port numarasını ayarlarWithDatabase(database string) Option: Veritabanı adını ayarlarWithUsername(username string) Option: Kullanıcı adını ayarlarWithPassword(password string) Option: Şifreyi ayarlarNewConnection(...Option) *Connection: Belirtilen seçeneklerle yeni bir bağlantı oluşturur
source://user:Extremely_SecurE_p@ssMorD@app:8081
Bu proje açık kaynaklıdır ve serbestçe kullanılabilir.
Katkılarınızı bekliyoruz! Lütfen pull request göndermeden önce değişikliklerinizi test edin.