0
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.

Sevy
  • 164
  • 16
  • 2
    Python is an interpreted language. Go is compiled. Parameter names are meaningless at runtime in Go. There is no way to do this in Go. – Burak Serdar Sep 19 '21 at 15:47
  • It's not possible in Go. See related: [Initialize function fields](https://stackoverflow.com/questions/48153929/initialize-function-fields/48154019#48154019) – icza Sep 19 '21 at 15:47

1 Answers1

1

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.

b01
  • 4,076
  • 2
  • 30
  • 30
Chandan
  • 11,465
  • 1
  • 6
  • 25
  • Yeah, that is a boss feature of Python. IMO the only way to go here is to use the User type, then use some slick way to convert your map to a User. Like add a method on the User type to take a map. – b01 Sep 19 '21 at 16:36
  • @CeriseLimón you are right the converting map to slice is not optimize solution but i want to show that it is also possible – Chandan Sep 19 '21 at 18:13
  • @Chandan In the slice example, it is not possible for the function to determine which value is the username, password or email. It's not that the example is sub-optimal. The example does not work. – Charlie Tumahai Sep 19 '21 at 19:30