Go 无法将字符串转换为*net.TCPlistener类型

Go 无法将字符串转换为*net.TCPlistener类型,go,Go,我想将字符串放入net.Listener,但出现错误: ./server.go:26:23: cannot use mainServer (type interface {}) as type string in argument to net.Listen: need type assertion ./server.go:27:28: cannot use gpServer (type interface {}) as type string in argument to net.Listen

我想将字符串放入net.Listener,但出现错误:

./server.go:26:23: cannot use mainServer (type interface {}) as type string in argument to net.Listen: need type assertion
./server.go:27:28: cannot use gpServer (type interface {}) as type string in argument to net.Listen: need type assertion
这是我的代码:

  viper.SetConfigFile("config.json")
  viper.AddConfigPath(".")
  viper.SetConfigName("config")
  viper.ReadInConfig()
  fmt.Printf("Using config: %s\n", viper.ConfigFileUsed())
  mainServer := viper.Get("mainServer.port")
  gpServer := viper.Get("gpServer.port")

  fmt.Println(mainServer,gpServer)



  fmt.Println("started main server")
  ln, _ := net.Listen("tcp", mainServer)
  gp_conn, _ := net.Listen("tcp", gpServer)
它是my config.json:

    "mainServer": {
        "host": "",
        "port": ":2323",
        "enabled": true
    },
    "gpServer": {
        "host": "",
        "port": ":3232",
        "enabled": true
    }
}

有人能解释一下如何将字符串转换为net.TCPListener类型吗? tnx

我相信viper.GetString方法比viper.Get更有效。基于文档:

但是,您可以直接断言类型。。。 这是不明智的,因为如果类型不是字符串,将导致恐慌

ln, _ := net.Listen("tcp", mainServer.(string))
gp_conn, _ := net.Listen("tcp", gpServer.(string))

它在错误中说:需要类型断言。还要检查文档,因为您正在使用的软件包可能有一个方便的方法来为您执行此操作。Tnx mad Wombat。。。我的问题已经解决了。。
ln, _ := net.Listen("tcp", mainServer.(string))
gp_conn, _ := net.Listen("tcp", gpServer.(string))