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']
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-27 11:00:00 +0000
Seen: 14 times
Last updated: Oct 21 '22
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?