Skip to content

RussellLuo/gopt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopt

Go Reference

Generic Functional Options for Go.

Installation

$ go get -u github.com/RussellLuo/gopt

Quick Start

package main

import (
	"fmt"

	"github.com/RussellLuo/gopt"
)

type Server struct {
	host string
	port int
}

func New(options ...gopt.Option[*Server]) *Server {
	return gopt.Apply(new(Server), options...)
}

// ServerOption holds all option factories for Server.
type ServerOption struct{}

func (ServerOption) Host(host string) gopt.Option[*Server] {
	return func(s *Server) { s.host = host }
}

func (ServerOption) Port(port int) gopt.Option[*Server] {
	return func(s *Server) { s.port = port }
}

func main() {
	server := New(
		ServerOption{}.Host("localhost"),
		ServerOption{}.Port(8080),
	)
	fmt.Printf("server: %+v\n", server)

	// Output:
	// server: &{host:localhost port:8080}
}

FAQ

  1. Why might I want to use this tiny library?

    Traditional Functional Options will define many exported functions, which is likely to cause naming conflicts.

    Reference articles:

    Reference projects:

  2. Why is ServerOption{} used in the example?

    ServerOption is a named alias of the empty struct, whose instances are fungible and consume no space.

Unsafe gopt

Unsafe gopt is an early implementation, which is deprecated and for reference only.

License

MIT

About

Generic Functional Options for Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages