1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/masx200-the-way-to-go_ZH_CN

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
twitter_status.go 1.2 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
marjune Отправлено 16.07.2019 05:57 00e4346
// twitter_status.go
package main
import (
"net/http"
"fmt"
"encoding/xml"
"io/ioutil"
)
/* these structs will house the unmarshalled response.
they should be hierarchically shaped like the XML
but can omit irrelevant data. */
type Status struct {
Text string
}
type User struct {
XMLName xml.Name
Status Status
}
// var user User
func main() {
// perform an HTTP request for the twitter status of user: Googland
resp, _ := http.Get("http://twitter.com/users/Googland.xml")
// initialize the structure of the XML response
user := User{xml.Name{"", "user"}, Status{""}}
// unmarshal the XML into our structures
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
fmt.Printf("error: %s", err.Error())
} else {
fmt.Printf("%s ---", body)
xml.Unmarshal(body, &user)
}
fmt.Printf("name: %s ", user.XMLName)
fmt.Printf("status: %s", user.Status.Text)
}
/* Output:
status: Robot cars invade California, on orders from Google: Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p<exit code="0" msg="process exited normally"/>
After Go1: no output: name: { user} status:
*/

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/masx200-the-way-to-go_ZH_CN.git
git@api.gitlife.ru:oschina-mirror/masx200-the-way-to-go_ZH_CN.git
oschina-mirror
masx200-the-way-to-go_ZH_CN
masx200-the-way-to-go_ZH_CN
master