Avoid writing same else statement in nested if/elseHow do you write multiline strings in Go?access golang websocket server with nodejs clientGo String after variable declarationGo reflect.MakeFunc. How to return a err=nil as reflect.Value?Managing errors in golangImprove multiple map access and avoid incorrect type conversionRace conditions in io.Pipe?Empty return in func with return value in golangIs it bad practice to reset an error in golang?Assign additional field when unmarshalling JSON object to GO struct

How much cash can I safely carry into the USA and avoid civil forfeiture?

Why do distances seem to matter in the Foundation world?

Could moose/elk survive in the Amazon forest?

Combinatorics problem, right solution?

How can I practically buy stocks?

Prove that the countable union of countable sets is also countable

Restricting the options of a lookup field, based on the value of another lookup field?

What does a straight horizontal line above a few notes, after a changed tempo mean?

Is there any pythonic way to find average of specific tuple elements in array?

How bug prioritization works in agile projects vs non agile

Which big number is bigger?

Retract an already submitted recommendation letter (written for an undergrad student)

Complex numbers z=-3-4i polar form

Will I lose my paid in full property

How exactly does Hawking radiation decrease the mass of black holes?

A ​Note ​on ​N!

Can I criticise the more senior developers around me for not writing clean code?

How to keep bees out of canned beverages?

Older movie/show about humans on derelict alien warship which refuels by passing through a star

A faster way to compute the largest prime factor

Negative Resistance

How to not starve gigantic beasts

What is the unit of time_lock_delta in LND?

What is this word supposed to be?



Avoid writing same else statement in nested if/else


How do you write multiline strings in Go?access golang websocket server with nodejs clientGo String after variable declarationGo reflect.MakeFunc. How to return a err=nil as reflect.Value?Managing errors in golangImprove multiple map access and avoid incorrect type conversionRace conditions in io.Pipe?Empty return in func with return value in golangIs it bad practice to reset an error in golang?Assign additional field when unmarshalling JSON object to GO struct






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








1















How do I avoid having to write the else statement twice in nested if/else statements?



func GetPortFromEnvironment(name string, defaultPort int32) int32 
var port int32
if env := os.Getenv("SERVICE_PORT"); env != ""
if _port, err := strconv.ParseInt(env, 10, 32); err != nil
port = int32(_port)
else
port = defaultPort

else
port = defaultPort

return port










share|improve this question






















  • flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

    – Paritosh Singh
    Mar 9 at 7:35











  • I guess the question is how do I join two different conditionals?

    – dimiguel
    Mar 9 at 7:36











  • Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

    – Paritosh Singh
    Mar 9 at 7:38

















1















How do I avoid having to write the else statement twice in nested if/else statements?



func GetPortFromEnvironment(name string, defaultPort int32) int32 
var port int32
if env := os.Getenv("SERVICE_PORT"); env != ""
if _port, err := strconv.ParseInt(env, 10, 32); err != nil
port = int32(_port)
else
port = defaultPort

else
port = defaultPort

return port










share|improve this question






















  • flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

    – Paritosh Singh
    Mar 9 at 7:35











  • I guess the question is how do I join two different conditionals?

    – dimiguel
    Mar 9 at 7:36











  • Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

    – Paritosh Singh
    Mar 9 at 7:38













1












1








1








How do I avoid having to write the else statement twice in nested if/else statements?



func GetPortFromEnvironment(name string, defaultPort int32) int32 
var port int32
if env := os.Getenv("SERVICE_PORT"); env != ""
if _port, err := strconv.ParseInt(env, 10, 32); err != nil
port = int32(_port)
else
port = defaultPort

else
port = defaultPort

return port










share|improve this question














How do I avoid having to write the else statement twice in nested if/else statements?



func GetPortFromEnvironment(name string, defaultPort int32) int32 
var port int32
if env := os.Getenv("SERVICE_PORT"); env != ""
if _port, err := strconv.ParseInt(env, 10, 32); err != nil
port = int32(_port)
else
port = defaultPort

else
port = defaultPort

return port







go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 7:30









dimigueldimiguel

775924




775924












  • flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

    – Paritosh Singh
    Mar 9 at 7:35











  • I guess the question is how do I join two different conditionals?

    – dimiguel
    Mar 9 at 7:36











  • Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

    – Paritosh Singh
    Mar 9 at 7:38

















  • flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

    – Paritosh Singh
    Mar 9 at 7:35











  • I guess the question is how do I join two different conditionals?

    – dimiguel
    Mar 9 at 7:36











  • Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

    – Paritosh Singh
    Mar 9 at 7:38
















flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

– Paritosh Singh
Mar 9 at 7:35





flat is better than nested. You can write both if statements into a single if with an and (or its equivalent in go language. wait, why am i here? goes back to python), and then you just have 1 if statement left. Also, in this example, you can return the port value early as well for the same effect.

– Paritosh Singh
Mar 9 at 7:35













I guess the question is how do I join two different conditionals?

– dimiguel
Mar 9 at 7:36





I guess the question is how do I join two different conditionals?

– dimiguel
Mar 9 at 7:36













Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

– Paritosh Singh
Mar 9 at 7:38





Google says && is the logical and in GO. I personally dont know this language im afraid, someone would have to confirm the same. Try using it?

– Paritosh Singh
Mar 9 at 7:38












1 Answer
1






active

oldest

votes


















3














I'll avoid using if which includes an optional statement:



if statement; condition 



and using simple if with:



func GetPortFromEnvironment(name string, defaultPort int32) int32 
env := os.Getenv("SERVICE_PORT")

// no env value. return defaultPort
if env == ""
return defaultPort


// there's env value.
port, err := strconv.ParseInt(env, 10, 32)
if err != nil

// Unable to parse port. Fallback to default.
return defaultPort


return int32(port)






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%2f55075062%2favoid-writing-same-else-statement-in-nested-if-else%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









    3














    I'll avoid using if which includes an optional statement:



    if statement; condition 



    and using simple if with:



    func GetPortFromEnvironment(name string, defaultPort int32) int32 
    env := os.Getenv("SERVICE_PORT")

    // no env value. return defaultPort
    if env == ""
    return defaultPort


    // there's env value.
    port, err := strconv.ParseInt(env, 10, 32)
    if err != nil

    // Unable to parse port. Fallback to default.
    return defaultPort


    return int32(port)






    share|improve this answer





























      3














      I'll avoid using if which includes an optional statement:



      if statement; condition 



      and using simple if with:



      func GetPortFromEnvironment(name string, defaultPort int32) int32 
      env := os.Getenv("SERVICE_PORT")

      // no env value. return defaultPort
      if env == ""
      return defaultPort


      // there's env value.
      port, err := strconv.ParseInt(env, 10, 32)
      if err != nil

      // Unable to parse port. Fallback to default.
      return defaultPort


      return int32(port)






      share|improve this answer



























        3












        3








        3







        I'll avoid using if which includes an optional statement:



        if statement; condition 



        and using simple if with:



        func GetPortFromEnvironment(name string, defaultPort int32) int32 
        env := os.Getenv("SERVICE_PORT")

        // no env value. return defaultPort
        if env == ""
        return defaultPort


        // there's env value.
        port, err := strconv.ParseInt(env, 10, 32)
        if err != nil

        // Unable to parse port. Fallback to default.
        return defaultPort


        return int32(port)






        share|improve this answer















        I'll avoid using if which includes an optional statement:



        if statement; condition 



        and using simple if with:



        func GetPortFromEnvironment(name string, defaultPort int32) int32 
        env := os.Getenv("SERVICE_PORT")

        // no env value. return defaultPort
        if env == ""
        return defaultPort


        // there's env value.
        port, err := strconv.ParseInt(env, 10, 32)
        if err != nil

        // Unable to parse port. Fallback to default.
        return defaultPort


        return int32(port)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 10:54









        Chris Drew

        11.9k22146




        11.9k22146










        answered Mar 9 at 8:13









        ישו אוהב אותךישו אוהב אותך

        16.5k84361




        16.5k84361





























            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%2f55075062%2favoid-writing-same-else-statement-in-nested-if-else%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

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко