我正在从用户那里获取一个物理位置地址,并试图安排它创建一个URL,以便以后使用它来获取来自Google Geocode API的JSON响应。如何替换Golang中的字符串中的单个字符?
最终URL字符串的结果应该是类似this one,无空格:
我不知道如何来代替我的URL字符串空格和逗号有代替。我看过一些关于字符串和正则表达式包,我创建了下面的代码:
package main
import (
"fmt"
"bufio"
"os"
"http"
)
func main() {
// Get the physical address
r := bufio.NewReader(os.Stdin)
fmt.Println("Enter a physical location address: ")
line, _, _ := r.ReadLine()
// Print the inputted address
address := string(line)
fmt.Println(address) // Need to see what I'm getting
// Create the URL and get Google's Geocode API JSON response for that address
URL := "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true"
fmt.Println(URL)
result, _ := http.Get(URL)
fmt.Println(result) // To see what I'm getting at this point
}
字符串是go中的不可变对象。所以你不能替换字符串中的字符。相反,你可以用替换的say片创建一个新的字符串。 – user510306