【发布时间】:2017-03-07 07:21:34
【问题描述】:
我正在用 golang 编写一些工具来让我的生活更轻松,但我完全不了解 net 包中的类型是如何工作的。这是我的代码的一部分:
import (
"bufio"
"fmt"
"log"
"net"
"os"
)
type configFile struct {
gateway, net net.IP
mask, port int
telnetUser, telnetPasswd string
}
var dataConfig configFile
func createConfigFile() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Let's fill te config file for the application.")
fmt.Println("Which is your gateway IP?")
readGateway, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
dataConfig.gateway = net.ParseIP(readGateway)
if dataConfig.gateway == nil {
log.Fatal("Problem here")
} else {
fmt.Println("Your gateway is: ", dataConfig.gateway.String())
}
}
我的问题是下一个:
我想从命令行读取一个 IP 地址并将其存储在 configFile 对象中,稍后我将使用该对象创建一个包含我程序的所有配置的 .json 文件。
当我从命令行读取 IP 地址时,readGateway 变量存储它没问题,这是意料之中的,但是当我尝试制作时
dataConfig.gateway = net.ParseIP(readGateway)
我尝试将字符串对象转换为net.IP 对象我总是在dataConfig.gateway 字段中得到nill,因此我无法使用该参数,也无法将其转换为字符串。
有人可以帮我吗? 提前致谢。
【问题讨论】:
-
如何将IP字符串传递给程序?你能粘贴你的 main() 函数吗?
-
主函数只调用了
createConfigFile()。我正在从标准输入读取 if。
标签: go