To change a []string in Go to a list in Python, you can use the following steps:
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']
Asked: 2021-05-27 11:00:00 +0000
Seen: 14 times
Last updated: Oct 21 '22