gogocql

Initialize gocql ips using a constant


I need to initialize gocql with multiple ips, I want to pass the ips from a variable/constant.

How to pass some thing like

gocql.NewCluster(ipvalues)

instead of using

gocql.NewCluster("127.0.0.1", "127.0.0.2")

i want to pass the list of ips through a variable something like an array.


Solution

  • As you can see, gocql.NewCluser takes a variadic parameter, which means you can pass multiple values separated with commas to the function.

    In go, you just need to make your ipvalues variable be a slice of strings and pass it like this:

    ipvalues := []string{"127.0.0.1", "127.0.0.2"}
    
    gocql.NewCluster(ipvalues...)
    

    This will have the same effect as writing gocql.NewCluster("127.0.0.1", "127.0.0.2")

    See the golang spec for more information on this feature