简体   繁体   中英

How to pass anonymous struct as function argument

The problem is that I can't access struct properties inside Converter function, but I can print em.

func main() {

    var io struct {
        Src   string
        Dest  string
    }

    flag.StringVar(&io.Src, "src", "temp_dir", "")
    flag.StringVar(&io.Dest, "dest", "users_dir", "")

    modules.Converter(&io)

}
// ./src/modules/converter.go
package modules

func Converter(io interface{}) {
    fmt.Println(io)
    // => {temp_dir users_dir}
}

What am I doing wrong? What is the right way to pass multiple props into function?

first, you need to change the io struct to Io

var Io struct {
       Src   string
       Dest  string
   }

And the function input should change this way

package modules

import "fmt"

func Converter(io *namePackage.Io) {
    fmt.Println(io.Src,io.Dest)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM