Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To change a []string in Go to a list in Python, you can use the following steps:

  1. Export the Go []string to a file (e.g. text file)
  2. Read the file in Python and parse it as a list

Here's an example of how this can be done:

In Go:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "strings"
)

func main() {
    // slice of strings
    names := []string{"Alice", "Bob", "Charlie"}
    fmt.Println(names)

    // create a text file
    err := ioutil.WriteFile("names.txt", []byte(fmt.Sprintf("%v", names)), 0644)
    if err != nil {
        log.Fatal(err)
    }
}

In Python:

with open("names.txt", "r") as f:
    # read contents of the file
    contents = f.read()

    # parse contents as a list
    names_list = contents.strip().replace("[", "").replace("]", "").replace("'", "").split(", ")

    # print the list
    print(names_list)

The output in Python will be:

['Alice', 'Bob', 'Charlie']