net包中的Golang类型

huangapple go评论86阅读模式
英文:

Golang types in net package

问题

我正在使用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("让我们填写应用程序的配置文件。")
    fmt.Println("你的网关IP是什么?")
    readGateway, err := reader.ReadString('\n')
    if err != nil {
        log.Fatal(err)
    }

    dataConfig.gateway = net.ParseIP(readGateway)

    if dataConfig.gateway == nil {
        log.Fatal("出现问题")
    } else {
        fmt.Println("你的网关是:", dataConfig.gateway.String())
    }

}

我的问题是:

我想从命令行读取一个IP地址,并将其存储在configFile对象中,稍后我将使用该对象创建一个包含程序所有配置的.json文件。

当我从命令行读取IP地址时,readGateway变量正确地存储了它,这是预期的,但是当我尝试执行dataConfig.gateway = net.ParseIP(readGateway)并将字符串对象转换为net.IP对象时,我始终在dataConfig.gateway字段中得到一个nil,因此我无法使用该参数,也无法将其转换为字符串。

有人可以帮助我吗?
提前感谢。

英文:

I'm coding in golang some tools to make my life easier and I'm not understanding at all how the types in the net package works. This is part of my code:

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())
	}

}

My problem is the next:

I want to read an IP address from the command line and storage it in the configFile object, which I will user later to create a .json file with all the configuration of my program.

When I read from the command line the IP address the readGateway variable storages it ok, that's expected, but when I try to make
dataConfig.gateway = net.ParseIP(readGateway)
and I try to cast the string object to a net.IP object I'm always getting a nill in the dataConfig.gateway field, so I'm not able to work with that parameter neither convert it to a string.

Could somebody help me?
Thanks in advance.

答案1

得分: 1

net.ParseIP(s string)的文档说明中提到,如果提供的字符串s不是有效的IP地址的文本表示形式,该函数将返回nil值,所以这一定是情况。

在调用net.ParseIP之前,请记录字符串s,以便您可以检查在程序中是否正确传递和读取它。

英文:

The documentation of net.ParseIP(s string) states that if the string s provided is not a valid textual representation of an IP address the function will return a nil value, so that must be the case.

Please log the string s before calling net.ParseIP so you can check if you are passing and reading it properly in the program.

答案2

得分: 1

bufio.Reader.ReadString的文档解释如下(重点标注为我的):

ReadString 从输入中读取直到第一个出现的分隔符(delim),返回一个包含数据的字符串,包括分隔符在内

因此,readGateway 最终会变成 "192.168.1.1\n"。换行符在一个格式正确的 IP 地址中是不存在的,这意味着当你使用 net.ParseIP 解析它时,它会将其排除为 nil

你可以使用 strings.TrimRight 去除换行符:

readGateway, err := reader.ReadString('\n')
if err != nil {
    log.Fatal(err)
}
readGateway = strings.TrimRight(readGateway, "\n")
英文:

bufio.Reader.ReadString's docs explain (emphasis mine)

> ReadString reads until the first occurrence of delim in the input,
> returning a string containing the data up to and including the
> delimiter

So readGateway ends up looking like "192.168.1.1\n". Your newline delimiter would not exist in a properly formatted IP address, which means when you parse it with net.ParseIP it's kicking it out as nil.

You can use strings.TrimRight to trim out the newline:

readGateway, err := reader.ReadString('\n')
if err != nil {
    log.Fatal(err)
}
readGateway = strings.TrimRight(readGateway, "\n")

huangapple
  • 本文由 发表于 2017年3月7日 06:22:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/42636702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定