英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论