Skip to content

Instantly share code, notes, and snippets.

@songfei1983
Created November 11, 2019 21:34
Show Gist options
  • Save songfei1983/669b59e56df7e3f96bc12a434341b760 to your computer and use it in GitHub Desktop.
Save songfei1983/669b59e56df7e3f96bc12a434341b760 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net"
"time"
)
// Server is struct
type Server struct {
listener net.Listener
Timeout time.Duration
}
// Config is struct
type Config struct {
Timeout time.Duration
}
// NewServer is func
func NewServer(addr string, options ...func(*Server)) (*Server, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
srv := Server{listener: l}
// ここまでは同じ
for _, option := range options {
option(&srv)
}
return &srv, nil
}
// Timeout is func
func Timeout(t int) func(*Server) {
return func(s *Server) {
s.Timeout = time.Duration(t) * time.Second
}
}
func main() {
srv, err := NewServer("localhost", Timeout(30))
if err != nil {
log.Fatal(err)
}
fmt.Println(srv.Timeout)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment