Files
bulma/flag/config_priority.go

33 lines
602 B
Go

package main
import (
"flag"
"fmt"
"os"
"strconv"
)
func main() {
port := flag.Int("port", 8080, "服务端口")
host := flag.String("host", "localhost", "服务绑定主机")
flag.Parse()
// 环境变量仅覆盖未手动传参的配置
if !flag.IsSet("port") {
if envPort := os.Getenv("SERVICE_PORT"); envPort != "" {
if p, err := strconv.Atoi(envPort); err == nil {
*port = p
}
}
}
if !flag.IsSet("host") {
if envHost := os.Getenv("SERVICE_HOST"); envHost != "" {
*host = envHost
}
}
fmt.Printf("最终生效配置:主机=%s,端口=%d\n", *host, *port)
}