doujiao7520
2017-03-06 22:22
浏览 17
已采纳

网包中的Golang类型

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('
')
    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.

图片转代码服务由CSDN问答提供 功能建议

我正在用golang编写一些工具,这些工具可以使我的生活更轻松,并且我根本不了解类型 在净包工程。 这是我的代码的一部分:

  import(
“ bufio” 
“ fmt” 
“ log” 
“ net” 
“ os” 
  } 
 
 
type configFile结构{
网关,net net.IP 
掩码,端口int 
 telnetUser,telnetPasswd字符串
} 
 
var dataConfig configFile 
 
func createConfigFile(){
读者 := bufio.NewReader(os.Stdin)
 fmt.Println(“让我们填写应用程序的配置文件。”)
 fmt.Println(“您的网关IP是哪个?”)
 readGateway,错误:=  reader.ReadString('
')
如果错误!= nil {
 log.Fatal(err)
} 
 
 dataConfig.gateway = net.ParseIP(readGateway)
 
如果dataConfig.gateway  == nil {
 log.Fatal(“问题在这里”)
}否则{
 fmt.Println(“您的网关是:”,dataConfig.gateway.String())
} 
 
} \  n   
 
 

我的问题是下一个:

我想从命令行读取IP地址并将其存储在configFile中 对象,稍后我将用它创建一个 .json 文件w

当我从命令行读取IP地址时, readGateway 变量可以正常存储,这是正常的,但是当我尝试 制作 dataConfig.gateway = net.ParseIP(readGateway) ,然后尝试将字符串对象转换为 net.IP 对象,但我总是得到< dataConfig.gateway 字段中的code> nill ,因此我无法使用该参数,也无法将其转换为字符串。

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drap5081683 2017-03-06 22:45
    已采纳

    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 ". 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('
    ')
    if err != nil {
        log.Fatal(err)
    }
    readGateway = strings.TrimRight(readGateway, "
    ")
    
    打赏 评论
  • douhe3313 2017-03-06 22:36

    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.

    打赏 评论

相关推荐 更多相似问题