{"record":{"id":"8c3bc50474b04f69","repo":"unknwon/the-way-to-go_ZH_CN","slug":"usage-host-port","errorCode":null,"errorMessage":"usage: host port","messagePattern":"usage: host port","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"eBook/15.1.md","lineNumber":229,"sourceCode":"示例 15.5 [simple_tcp_server.go](examples/chapter_15/simple_tcp_server.go)：\n\n```go\n// Simple multi-thread/multi-core TCP server.\npackage main\n\nimport (\n\t\"flag\"\n\t\"fmt\"\n\t\"net\"\n\t\"os\"\n)\n\nconst maxRead = 25\n\nfunc main() {\n\tflag.Parse()\n\tif flag.NArg() != 2 {\n\t\tpanic(\"usage: host port\")\n\t}\n\thostAndPort := fmt.Sprintf(\"%s:%s\", flag.Arg(0), flag.Arg(1))\n\tlistener := initServer(hostAndPort)\n\tfor {\n\t\tconn, err := listener.Accept()\n\t\tcheckError(err, \"Accept: \")\n\t\tgo connectionHandler(conn)\n\t}\n}\n\nfunc initServer(hostAndPort string) *net.TCPListener {\n\tserverAddr, err := net.ResolveTCPAddr(\"tcp\", hostAndPort)\n\tcheckError(err, \"Resolving address:port failed: '\"+hostAndPort+\"'\")\n\tlistener, err := net.ListenTCP(\"tcp\", serverAddr)\n\tcheckError(err, \"ListenTCP: \")\n\tprintln(\"Listening to: \", listener.Addr().String())\n\treturn listener\n}","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/unknwon/the-way-to-go_ZH_CN/blob/7a54d34d3657084b6a59e5618bd069b912d571aa/eBook/15.1.md#L211-L247","documentation":"Usage guard at the top of the simple TCP server main (section 15.1): after flag.Parse(), if flag.NArg() != 2 the program panics with 'usage: host port', demanding exactly two positional arguments (host and port) that are later combined with fmt.Sprintf(\"%s:%s\", flag.Arg(0), flag.Arg(1)).","triggerScenarios":"Running the binary with zero, one, or three-plus positional arguments — e.g. a plain 'go run simple_tcp_server.go' with nothing after the file, or flags that shift the positional count away from 2.","commonSituations":"First run from an IDE whose run configuration has no program arguments; forgetting the port; copying an invocation line from docs that used a different argument layout.","solutions":["Run with both arguments: go run simple_tcp_server.go localhost 5000","Replace the panic with flag.Usage() plus log.Fatalf(\"usage: %s host port\", os.Args[0]) for a proper usage message and exit code","Define host/port as flag.String with sensible defaults so missing args fall back instead of aborting","Validate that the port is numeric before passing it to net.ResolveTCPAddr"],"exampleFix":"// before\nif flag.NArg() != 2 {\n    panic(\"usage: host port\")\n}\n\n// after\nif flag.NArg() != 2 {\n    flag.Usage()\n    log.Fatalf(\"usage: %s host port\", os.Args[0])\n}","handlingStrategy":"validation","validationCode":"// check invocation before anything touches the network\nhost, portStr, ok := os.Args[1], \"\", len(os.Args) == 3\nif !ok {\n    fmt.Fprintf(os.Stderr, \"usage: %s host port\\n\", os.Args[0])\n    os.Exit(2)\n}\nif _, err := strconv.Atoi(os.Args[2]); err != nil {\n    fmt.Fprintf(os.Stderr, \"port must be numeric: %s\\n\", os.Args[2])\n    os.Exit(2)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Configure program arguments in the IDE run configuration before hitting Run","Prefer flag.String with defaults over positional-arg panics","Print flag.Usage and exit with code 2 instead of panicking on bad invocation"],"tags":["go","cli","flags","usage","panic","tcp-server"],"backgroundTag":null,"analyzedSha":"7a54d34d3657084b6a59e5618bd069b912d571aa","analyzedAt":"2026-08-15T15:13:06.026Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}