我可以将映射的值解包为函数参数吗?

huangapple go评论88阅读模式
英文:

Can I unpack map values as function arguments?

问题

CreateUser(user, email, password string)
credentials := map[string]string{
    "username": query.Get("username"),
    "password": query.Get("password"),
    "email": query.Get("email"),
}

userRepository.CreateUser(credentials)

CreateUser 函数需要 user、password 和 email 参数。在 Python 中,可以通过在变量名前添加两个星号,并使用每个键作为参数名的方式,将字典传递给函数并进行“解包”。但是我找不到在 Go 中的类似方法。

英文:
CreateUser(user, email, password string)
credentials := map[string]string{
    "username": query.Get("username"),
    "password": query.Get("password"),
    "email": query.Get("email"),
  }

userRepository.CreateUser(credentials)

CreateUser function is expecting user, password and email arguments. There's easy way to pass dictionary to a function and "unpack" by adding two stars before variable's name and use each key as name of argument in Python but I can't find answer for Go.

答案1

得分: 1

根据@BurakSerdar和@icza的说法,在Go语言中无法像Python那样在函数中传递扩展参数。

你可以直接在func中使用map[string]string类型的参数,例如:

package main

import (
	"fmt"
)

func CreateUser(data map[string]string) {
  fmt.Println(data["username"], data["password"], data["email"])
}

func main() {
	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	CreateUser(credentials)
}

这是一个可运行的示例

或者你可以使用结构体来传递数据给func,例如:

package main

import (
	"fmt"
)

type User struct {
	Name     string
	Password string
	Email    string
}

func CreateUser(user User) {
	fmt.Println(user.Name, user.Password, user.Email)
}

func main() {
	credentials := User{
		Name:     "John",
		Password: "Kenny",
		Email:    "john@kenny.com",
	}

	CreateUser(credentials)
}

这是一个可运行的示例

或者你可以使用接口来传递数据给func,例如:

package main

import (
	"fmt"
)

func CreateUser(m interface{}) {
  data, _ := m.(map[string]string)
  fmt.Println(data["username"], data["password"], data["email"])
}

func main() {
	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	CreateUser(credentials)
}

这是一个可运行的示例

或者你可以使用带有扩展操作符的数组来传递数据给func,例如:

package main

import (
	"fmt"
)

func CreateUser(args ...string) {
	fmt.Println(args[0], args[1], args[2])
}

func main() {

	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	v := make([]string, 0, len(credentials))
	for _, value := range credentials {
		v = append(v, value)
	}

	CreateUser(v...)
}

这是一个可运行的示例

这里还有一个在上述User类型示例基础上构建的可运行示例

英文:

As @BurakSerdar and @icza stated it is not possible in Go to pass spread parameters in function like in Python

You can directly use map[string]string parameter with func

package main

import (
	"fmt"
)

func CreateUser(data map[string]string) {
  fmt.Println(data["username"], data["password"], data["email"])
}

func main() {
	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	CreateUser(credentials)
}

Here working example

Or You can use struct to pass data to func

package main

import (
	"fmt"
)

type User struct {
	Name     string
	Password string
	Email    string
}

func CreateUser(user User) {
	fmt.Println(user.Name, user.Password, user.Email)
}
func main() {
	credentials := User{
		Name: "John",
		Password: "Kenny",
		Email:    "john@kenny.com",
	}

	CreateUser(credentials)
}

Here working example

Or You can use interface to pass data to func

package main

import (
	"fmt"
)

func CreateUser(m interface{}) {
  data, _ := m.(map[string]string)
  fmt.Println(data["username"], data["password"], data["email"])
}

func main() {
	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	CreateUser(credentials)
}

Here working example

Or You can use array with spread operator to pass data to func

package main

import (
	"fmt"
)

func CreateUser(args ...string) {
	fmt.Println(args[0], args[1], args[2])
}

func main() {

	credentials := map[string]string{
		"username": "John",
		"password": "Kenny",
		"email":    "john@kenny.com",
	}

	v := make([]string, 0, len(credentials))
	for _, value := range credentials {
		v = append(v, value)
	}

	CreateUser(v...)
}

Here working example

Here's a working example that builds on the User type example above.

huangapple
  • 本文由 发表于 2021年9月19日 23:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/69244755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定