Created
November 11, 2019 21:34
-
-
Save songfei1983/669b59e56df7e3f96bc12a434341b760 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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