How to pass [] string as...interface{} parameter


I understand:

I know when the parameter is... strings

But this case is a little different:

func main() {
    args := []string{"hello", "world"}
    fmt.Println(args...)
}

https://play.golang.org/p/7ELdQQvdPvR

The above code throws the errorcannot use args (type [] string) as type [] interface {} in argument to fmt.Println

What is the most idiomatic way to achieve this?

Burak Serdar:

This can be done in one of two ways:

   args := []interface{}{"hello", "world"}
   fmt.Println(args...)

either:

  args:=[]string{"hello", "world"}
  iargs:=make([]interface{},0)
  for _,x:=range args {
     iargs=append(iargs, x)
  }
  fmt.Println(iargs...)

You can pass one stringof which interface{}is required, but you cannot pass []stringone of which is []interface{}required. The compiler converts value stringto value interface{}, but that's not what's done with arrays, you have to do the conversion yourself.

Related


How to pass [] string as...interface{} parameter

I understand: I know when the parameter is... strings But this case is a little different: func main() { args := []string{"hello", "world"} fmt.Println(args...) } https://play.golang.org/p/7ELdQQvdPvR The above code throws the errorcannot use args (ty

How to pass part of *interface{} as parameter?

MakotoE: I would like to use in a package , but the number of columns and therefore the number of parameters will change at runtime. Here is the signature of :Scan()sqlScan() func (rs *Rows) Scan(dest ...interface{}) error According to the documentation, *int

How to pass part of *interface{} as parameter?

MakotoE: I would like to use in a package , but the number of columns and therefore the number of parameters will change at runtime. Here is the signature of :Scan()sqlScan() func (rs *Rows) Scan(dest ...interface{}) error According to the documentation, *int

How to pass part of *interface{} as parameter?

MakotoE: I would like to use in a package , but the number of columns and therefore the number of parameters will change at runtime. Here is the signature of :Scan()sqlScan() func (rs *Rows) Scan(dest ...interface{}) error According to the documentation, *int

How to pass part of *interface{} as parameter?

MakotoE: I would like to use in a package , but the number of columns and therefore the number of parameters will change at runtime. Here is the signature of :Scan()sqlScan() func (rs *Rows) Scan(dest ...interface{}) error According to the documentation, *int

Pass string slice to variadic empty interface parameter

Paul Ruane The package gosqlite I'm using has methods with variadic parameters of type empty interface . func (s *Stmt) Exec(args ...interface{}) os.Error If I pass the individual parameters explicitly, I can call it as well: statement := blah() error := stat

Pass string slice to variadic empty interface parameter

Paul Ruane The package gosqlite I'm using has methods with variadic parameters of type empty interface . func (s *Stmt) Exec(args ...interface{}) os.Error If I pass the individual parameters explicitly, I can call it as well: statement := blah() error := stat

Pass string slice to variadic empty interface parameter

Paul Ruane The package gosqlite I'm using has methods with variadic parameters of type empty interface . func (s *Stmt) Exec(args ...interface{}) os.Error If I pass the individual parameters explicitly, I can call it as well: statement := blah() error := stat

How to pass interface as parameter in WCF service?

Tyler Buchanan I have the following, but I'm not sure if this is the correct approach. namespace WCFServices { [ServiceContract(Name = "IService")] [ServiceKnownTypeAttribute(typeof(DataItem))] public interface IService { [OperationCont

How to pass interface as parameter in WCF service?

Tyler Buchanan I have the following, but I'm not sure if this is the correct approach. namespace WCFServices { [ServiceContract(Name = "IService")] [ServiceKnownTypeAttribute(typeof(DataItem))] public interface IService { [OperationCont

How to pass interface as parameter in WCF service?

Tyler Buchanan I have the following, but I'm not sure if this is the correct approach. namespace WCFServices { [ServiceContract(Name = "IService")] [ServiceKnownTypeAttribute(typeof(DataItem))] public interface IService { [OperationCont

How to pass interface as parameter in WCF service?

Tyler Buchanan I have the following, but I'm not sure if this is the correct approach. namespace WCFServices { [ServiceContract(Name = "IService")] [ServiceKnownTypeAttribute(typeof(DataItem))] public interface IService { [OperationCont

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass string in url parameter?

not any From API i get something like this: processId=22, now i want to pass it in url parameter but the problem is i need to pass key and value. How can i paste the whole string as parameter. Any suggestions? So what I want to achieve is: <a *ngIf="menu.refPa

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass multiline String parameter?

username Suppose I have this method: def read_line_by_line(some_text) some_text.each |line| do (something) end end How can I do this? I have got: my first line of the input text I tried to pass it as a parameter but got a weird output. It doesn't read line

How to pass string parameter in innerhtml

Khurshid Ansari I am creating dynamic html basic server response. example: var responseServer = { name: "al the' too" } var htmlView = `<div onclick="info(responseServer.name)"> </div>`; //Error: al identifier is not defined. var htmlView = `<div onclick="

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass multiline String parameter?

username Suppose I have this method: def read_line_by_line(some_text) some_text.each |line| do (something) end end How can I do this? I have got: my first line of the input text I tried to pass it as a parameter but got a weird output. It doesn't read line

How to pass string parameter in java as parameter at runtime

username I am new to Java and I need help because how to get the value at runtime when executing a Java file. I have a Java program where the values of host, user, password and command are hardcoded, how can I parameterize them. E.g, public static void mai

How to pass string parameter in java as parameter at runtime

username I am new to Java and I need help because how to get the value at runtime when executing a Java file. I have a Java program where the values of host, user, password and command are hardcoded, how can I parameterize them. E.g, public static void mai