Skip to content

n3xpect/functional-options-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Functional Options Pattern in Go

A sample Go project demonstrating connection configuration using the functional options pattern.

Description

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.

Features

  • Functional options pattern implementation
  • Flexible configuration system
  • Default values support
  • Connection string generation
  • Multiple configuration options (server, port, database, username, password)

Installation

Clone the repository:

git clone <repository-url>
cd functional-options-go

Usage

To run the project:

go run main.go

Example Usage

// 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))

Project Structure

functional-options-go/
├── main.go      # Main application code
├── go.mod       # Go module definitions
└── README.md    # This file

Configuration

Default Values

  • Server: localhost
  • Port: 9715
  • Database: test
  • Username: root
  • Password: root

Configuration Functions

  • WithServer(host string) Option: Sets the server address
  • WithPort(port int) Option: Sets the port number
  • WithDatabase(database string) Option: Sets the database name
  • WithUsername(username string) Option: Sets the username
  • WithPassword(password string) Option: Sets the password
  • NewConnection(...Option) *Connection: Creates a new connection with the provided options

Example Output

source://user:Extremely_SecurE_p@ssMorD@app:8081

License

This project is open source and freely available for use.

Contributing

Contributions are welcome! Please test your changes before submitting a pull request.







Go'da Functional Options Pattern

Go dilinde functional options pattern kullanarak bağlantı yapılandırması yapan bir örnek proje.

Açıklama

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.

Özellikler

  • 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)

Kurulum

Projeyi klonlayın:

git clone <repository-url>
cd functional-options-go

Kullanım

Projeyi çalıştırmak için:

go run main.go

Örnek Kullanım

// 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))

Proje Yapısı

functional-options-go/
├── main.go      # Ana uygulama kodu
├── go.mod       # Go modül tanımları
└── README.md    # Bu dosya

Yapılandırma

Varsayılan Değerler

  • Server: localhost
  • Port: 9715
  • Database: test
  • Username: root
  • Password: root

Yapılandırma Fonksiyonları

  • WithServer(host string) Option: Sunucu adresini ayarlar
  • WithPort(port int) Option: Port numarasını ayarlar
  • WithDatabase(database string) Option: Veritabanı adını ayarlar
  • WithUsername(username string) Option: Kullanıcı adını ayarlar
  • WithPassword(password string) Option: Şifreyi ayarlar
  • NewConnection(...Option) *Connection: Belirtilen seçeneklerle yeni bir bağlantı oluşturur

Örnek Çıktı

source://user:Extremely_SecurE_p@ssMorD@app:8081

Lisans

Bu proje açık kaynaklıdır ve serbestçe kullanılabilir.

Katkıda Bulunma

Katkılarınızı bekliyoruz! Lütfen pull request göndermeden önce değişikliklerinizi test edin.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages