cant convert String to *net.TCPlistener typeHow to efficiently concatenate strings in Go?Convert string to integer type in Go?How do you write multiline strings in Go?How to convert an int value to string in Go?Type converting slices of interfaces in GoHow to convert a zero-terminated byte array to string?Type conversion of string to intGO: Type assertion from listWhy does putting a pointer in an interface in Go cause reflect to lose the name of the type?Use reflect for set value of interface type

How did Arya manage the sneak attack?

Reverse the word in a string with the same order in javascript

Is it possible to Ready a spell to be cast just before the start of your next turn by having the trigger be an ally's attack?

Please, smoke with good manners

Do I have an "anti-research" personality?

Why does nature favour the Laplacian?

Python "triplet" dictionary?

Single Colour Mastermind Problem

Volunteering in England

Subtleties of choosing the sequence of tenses in Russian

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

Build a trail cart

Does jamais mean always or never in this context?

Is GOCE a satellite or aircraft?

How does a Swashbuckler rogue "fight with two weapons while safely darting away"?

Stark VS Thanos

Why “le” behind?

How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?

What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?

When to use 1/Ka vs Kb

Pulling the rope with one hand is as heavy as with two hands?

Why does processed meat contain preservatives, while canned fish needs not?

Pressure to defend the relevance of one's area of mathematics

Modify locally tikzset



cant convert String to *net.TCPlistener type


How to efficiently concatenate strings in Go?Convert string to integer type in Go?How do you write multiline strings in Go?How to convert an int value to string in Go?Type converting slices of interfaces in GoHow to convert a zero-terminated byte array to string?Type conversion of string to intGO: Type assertion from listWhy does putting a pointer in an interface in Go cause reflect to lose the name of the type?Use reflect for set value of interface type






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















i want to put string in net.Listener but i got error :



./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


its my code :



 viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.ReadInConfig()
fmt.Printf("Using config: %sn", 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)


its my config.json :



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

}



can some one explain me how i can convert string to net.TCPListener Type ?
tnx










share|improve this question






















  • It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

    – JimB
    Mar 22 at 19:37











  • godoc.org/github.com/spf13/viper#GetString

    – Mad Wombat
    Mar 22 at 19:53











  • Tnx "mad Wombat" ... My problem has been resolved ..

    – MahdiNajafi
    Mar 22 at 20:05

















-1















i want to put string in net.Listener but i got error :



./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


its my code :



 viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.ReadInConfig()
fmt.Printf("Using config: %sn", 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)


its my config.json :



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

}



can some one explain me how i can convert string to net.TCPListener Type ?
tnx










share|improve this question






















  • It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

    – JimB
    Mar 22 at 19:37











  • godoc.org/github.com/spf13/viper#GetString

    – Mad Wombat
    Mar 22 at 19:53











  • Tnx "mad Wombat" ... My problem has been resolved ..

    – MahdiNajafi
    Mar 22 at 20:05













-1












-1








-1








i want to put string in net.Listener but i got error :



./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


its my code :



 viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.ReadInConfig()
fmt.Printf("Using config: %sn", 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)


its my config.json :



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

}



can some one explain me how i can convert string to net.TCPListener Type ?
tnx










share|improve this question














i want to put string in net.Listener but i got error :



./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


its my code :



 viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.ReadInConfig()
fmt.Printf("Using config: %sn", 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)


its my config.json :



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

}



can some one explain me how i can convert string to net.TCPListener Type ?
tnx







go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 19:13









MahdiNajafiMahdiNajafi

11




11












  • It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

    – JimB
    Mar 22 at 19:37











  • godoc.org/github.com/spf13/viper#GetString

    – Mad Wombat
    Mar 22 at 19:53











  • Tnx "mad Wombat" ... My problem has been resolved ..

    – MahdiNajafi
    Mar 22 at 20:05

















  • It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

    – JimB
    Mar 22 at 19:37











  • godoc.org/github.com/spf13/viper#GetString

    – Mad Wombat
    Mar 22 at 19:53











  • Tnx "mad Wombat" ... My problem has been resolved ..

    – MahdiNajafi
    Mar 22 at 20:05
















It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

– JimB
Mar 22 at 19:37





It says right in the error: need type assertion. Also check the docs, because the package you're using probably has a convenience method to do this for you.

– JimB
Mar 22 at 19:37













godoc.org/github.com/spf13/viper#GetString

– Mad Wombat
Mar 22 at 19:53





godoc.org/github.com/spf13/viper#GetString

– Mad Wombat
Mar 22 at 19:53













Tnx "mad Wombat" ... My problem has been resolved ..

– MahdiNajafi
Mar 22 at 20:05





Tnx "mad Wombat" ... My problem has been resolved ..

– MahdiNajafi
Mar 22 at 20:05












1 Answer
1






active

oldest

votes


















0














I believe the method viper.GetString would work better than viper.Get based on the documentation:
fyi, you can take a look at these things here: https://godoc.org/github.com/spf13/viper



mainServer := viper.GetString("mainServer.port")
gpServer := viper.GetString("gpServer.port")


However, you could assert the type directly...
Bear in mind this is ill-advised, you shouldn't do this as it will result in a panic if the type is not a string AND viper has the convenience method used above.



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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306395%2fcant-convert-string-to-net-tcplistener-type%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I believe the method viper.GetString would work better than viper.Get based on the documentation:
    fyi, you can take a look at these things here: https://godoc.org/github.com/spf13/viper



    mainServer := viper.GetString("mainServer.port")
    gpServer := viper.GetString("gpServer.port")


    However, you could assert the type directly...
    Bear in mind this is ill-advised, you shouldn't do this as it will result in a panic if the type is not a string AND viper has the convenience method used above.



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





    share|improve this answer



























      0














      I believe the method viper.GetString would work better than viper.Get based on the documentation:
      fyi, you can take a look at these things here: https://godoc.org/github.com/spf13/viper



      mainServer := viper.GetString("mainServer.port")
      gpServer := viper.GetString("gpServer.port")


      However, you could assert the type directly...
      Bear in mind this is ill-advised, you shouldn't do this as it will result in a panic if the type is not a string AND viper has the convenience method used above.



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





      share|improve this answer

























        0












        0








        0







        I believe the method viper.GetString would work better than viper.Get based on the documentation:
        fyi, you can take a look at these things here: https://godoc.org/github.com/spf13/viper



        mainServer := viper.GetString("mainServer.port")
        gpServer := viper.GetString("gpServer.port")


        However, you could assert the type directly...
        Bear in mind this is ill-advised, you shouldn't do this as it will result in a panic if the type is not a string AND viper has the convenience method used above.



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





        share|improve this answer













        I believe the method viper.GetString would work better than viper.Get based on the documentation:
        fyi, you can take a look at these things here: https://godoc.org/github.com/spf13/viper



        mainServer := viper.GetString("mainServer.port")
        gpServer := viper.GetString("gpServer.port")


        However, you could assert the type directly...
        Bear in mind this is ill-advised, you shouldn't do this as it will result in a panic if the type is not a string AND viper has the convenience method used above.



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 19:59









        snassrsnassr

        44058




        44058





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306395%2fcant-convert-string-to-net-tcplistener-type%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript