我正在從用戶那裏獲取一個物理位置地址,並試圖安排它創建一個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