How to create a dynamic variable name by concatenating two strings












-2















I want to a create variable name by concatenating with current date and provide a value to the created variable name. My variable name should look something like this "Key-2019-01" so that I can store the value as var Key-2019-01 = "yes"



I have tried like the below.



     package main

import (
"fmt"
"time"
"strconv"
"strings"
)

func main() {
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
var month = int(currentMonth)
var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

var "Key",currentDate string

value, err := json.Marshal("yes")
stub.PutState(("Key", currentDate), value)
}


It could be helpful if I get a working code of this.










share|improve this question

























  • no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

    – Priyanka
    Jan 20 at 10:12













  • Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

    – Archer
    Jan 20 at 10:12













  • is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

    – Priyanka
    Jan 20 at 10:16











  • This is not possible in Go.

    – Flimzy
    Jan 20 at 10:18











  • ok thank you @Flimzy

    – Priyanka
    Jan 20 at 10:20
















-2















I want to a create variable name by concatenating with current date and provide a value to the created variable name. My variable name should look something like this "Key-2019-01" so that I can store the value as var Key-2019-01 = "yes"



I have tried like the below.



     package main

import (
"fmt"
"time"
"strconv"
"strings"
)

func main() {
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
var month = int(currentMonth)
var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

var "Key",currentDate string

value, err := json.Marshal("yes")
stub.PutState(("Key", currentDate), value)
}


It could be helpful if I get a working code of this.










share|improve this question

























  • no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

    – Priyanka
    Jan 20 at 10:12













  • Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

    – Archer
    Jan 20 at 10:12













  • is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

    – Priyanka
    Jan 20 at 10:16











  • This is not possible in Go.

    – Flimzy
    Jan 20 at 10:18











  • ok thank you @Flimzy

    – Priyanka
    Jan 20 at 10:20














-2












-2








-2








I want to a create variable name by concatenating with current date and provide a value to the created variable name. My variable name should look something like this "Key-2019-01" so that I can store the value as var Key-2019-01 = "yes"



I have tried like the below.



     package main

import (
"fmt"
"time"
"strconv"
"strings"
)

func main() {
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
var month = int(currentMonth)
var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

var "Key",currentDate string

value, err := json.Marshal("yes")
stub.PutState(("Key", currentDate), value)
}


It could be helpful if I get a working code of this.










share|improve this question
















I want to a create variable name by concatenating with current date and provide a value to the created variable name. My variable name should look something like this "Key-2019-01" so that I can store the value as var Key-2019-01 = "yes"



I have tried like the below.



     package main

import (
"fmt"
"time"
"strconv"
"strings"
)

func main() {
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
var month = int(currentMonth)
var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

var "Key",currentDate string

value, err := json.Marshal("yes")
stub.PutState(("Key", currentDate), value)
}


It could be helpful if I get a working code of this.







variables go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 10:18









Flimzy

38.5k96597




38.5k96597










asked Jan 20 at 10:02









PriyankaPriyanka

117




117













  • no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

    – Priyanka
    Jan 20 at 10:12













  • Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

    – Archer
    Jan 20 at 10:12













  • is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

    – Priyanka
    Jan 20 at 10:16











  • This is not possible in Go.

    – Flimzy
    Jan 20 at 10:18











  • ok thank you @Flimzy

    – Priyanka
    Jan 20 at 10:20



















  • no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

    – Priyanka
    Jan 20 at 10:12













  • Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

    – Archer
    Jan 20 at 10:12













  • is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

    – Priyanka
    Jan 20 at 10:16











  • This is not possible in Go.

    – Flimzy
    Jan 20 at 10:18











  • ok thank you @Flimzy

    – Priyanka
    Jan 20 at 10:20

















no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

– Priyanka
Jan 20 at 10:12







no this is golang.if there is way for this in solution in javascript. i thought of trying the logic in golang

– Priyanka
Jan 20 at 10:12















Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

– Archer
Jan 20 at 10:12







Dynamic variable name is not what you want. Have a look at a key/value pair... gobyexample.com/maps (Also I removed the irrelevant tags)

– Archer
Jan 20 at 10:12















is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

– Priyanka
Jan 20 at 10:16





is there a possibility that i can create a variable name by concatenating with current date and assign a value to the variable?

– Priyanka
Jan 20 at 10:16













This is not possible in Go.

– Flimzy
Jan 20 at 10:18





This is not possible in Go.

– Flimzy
Jan 20 at 10:18













ok thank you @Flimzy

– Priyanka
Jan 20 at 10:20





ok thank you @Flimzy

– Priyanka
Jan 20 at 10:20












1 Answer
1






active

oldest

votes


















1














This is not possible in Go, since it's a compiled and statically typed language. The compiler has to know, at compile-time, the names and types of variables (*). What you ask is a run-time variable naming, which cannot work. However, it's extremely unlikely that you actually need this in practice. Maps can provide a similar capability:



m := make(map[string]string)
m["key"] = "value"


And you can construct either of these at run-time. Slightly adapting your example:



package main

import (
"fmt"
"strconv"
"strings"
"time"
)

func main() {
m := make(map[string]string)
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
var month = int(currentMonth)
var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

m["key"+currentDate] = "value"
fmt.Println(m)
}


Prints out:



map[key2019-1:value]




(*) With some exceptions that are not relevant for this particular discussion.






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%2f54275337%2fhow-to-create-a-dynamic-variable-name-by-concatenating-two-strings%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









    1














    This is not possible in Go, since it's a compiled and statically typed language. The compiler has to know, at compile-time, the names and types of variables (*). What you ask is a run-time variable naming, which cannot work. However, it's extremely unlikely that you actually need this in practice. Maps can provide a similar capability:



    m := make(map[string]string)
    m["key"] = "value"


    And you can construct either of these at run-time. Slightly adapting your example:



    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    )

    func main() {
    m := make(map[string]string)
    currentMonth := time.Now().Month()
    currentYear := time.Now().Year()
    var month = int(currentMonth)
    var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

    m["key"+currentDate] = "value"
    fmt.Println(m)
    }


    Prints out:



    map[key2019-1:value]




    (*) With some exceptions that are not relevant for this particular discussion.






    share|improve this answer




























      1














      This is not possible in Go, since it's a compiled and statically typed language. The compiler has to know, at compile-time, the names and types of variables (*). What you ask is a run-time variable naming, which cannot work. However, it's extremely unlikely that you actually need this in practice. Maps can provide a similar capability:



      m := make(map[string]string)
      m["key"] = "value"


      And you can construct either of these at run-time. Slightly adapting your example:



      package main

      import (
      "fmt"
      "strconv"
      "strings"
      "time"
      )

      func main() {
      m := make(map[string]string)
      currentMonth := time.Now().Month()
      currentYear := time.Now().Year()
      var month = int(currentMonth)
      var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

      m["key"+currentDate] = "value"
      fmt.Println(m)
      }


      Prints out:



      map[key2019-1:value]




      (*) With some exceptions that are not relevant for this particular discussion.






      share|improve this answer


























        1












        1








        1







        This is not possible in Go, since it's a compiled and statically typed language. The compiler has to know, at compile-time, the names and types of variables (*). What you ask is a run-time variable naming, which cannot work. However, it's extremely unlikely that you actually need this in practice. Maps can provide a similar capability:



        m := make(map[string]string)
        m["key"] = "value"


        And you can construct either of these at run-time. Slightly adapting your example:



        package main

        import (
        "fmt"
        "strconv"
        "strings"
        "time"
        )

        func main() {
        m := make(map[string]string)
        currentMonth := time.Now().Month()
        currentYear := time.Now().Year()
        var month = int(currentMonth)
        var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

        m["key"+currentDate] = "value"
        fmt.Println(m)
        }


        Prints out:



        map[key2019-1:value]




        (*) With some exceptions that are not relevant for this particular discussion.






        share|improve this answer













        This is not possible in Go, since it's a compiled and statically typed language. The compiler has to know, at compile-time, the names and types of variables (*). What you ask is a run-time variable naming, which cannot work. However, it's extremely unlikely that you actually need this in practice. Maps can provide a similar capability:



        m := make(map[string]string)
        m["key"] = "value"


        And you can construct either of these at run-time. Slightly adapting your example:



        package main

        import (
        "fmt"
        "strconv"
        "strings"
        "time"
        )

        func main() {
        m := make(map[string]string)
        currentMonth := time.Now().Month()
        currentYear := time.Now().Year()
        var month = int(currentMonth)
        var currentDate = strings.Join(string{strconv.Itoa(currentYear), "-", strconv.Itoa(month)}, "")

        m["key"+currentDate] = "value"
        fmt.Println(m)
        }


        Prints out:



        map[key2019-1:value]




        (*) With some exceptions that are not relevant for this particular discussion.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 14:37









        Eli BenderskyEli Bendersky

        166k67298371




        166k67298371
































            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%2f54275337%2fhow-to-create-a-dynamic-variable-name-by-concatenating-two-strings%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

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre