golang 在Golang中即将举办的Meetup活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 在Golang中即将举办的Meetup活动相关的知识,希望对你有一定的参考价值。

package main

import "net/http"
import "log"
import "os"
import "io/ioutil"
import "encoding/json"

// import "time"

type MeetupEventCollection struct {
	Results []MeetupEvent
	meta    EventResultsMetaData
}

type EventResultsMetaData struct {
	Lon         string
	Count       int
	Description string
	Id          string
	Lat         string
	Link        string
	Method      string
	Next        string
	Title       string
	Total_count int
	Updated     float64
	Url         string
}

type MeetupEvent struct {
	id               string
	Name             string
	description      string
	rsvp_limit       int
	status           string
	visibility       string
	maybe_rsvp_count int
	Venue            EventVenue
	fee              EventFee
	group            EventGroup
	Utc_offset       float64
	Time             float64
	waitlist_count   int
	updated          float64
	yes_rsvp_count   int
	created          float64
	Event_url        string
	headcount        int
}

type EventVenue struct {
	id        float64
	lon       float64
	lat       float64
	repinned  bool
	Name      string
	state     string
	Address_1 string
	City      string
	country   string
}

type EventFee struct {
	Amount      float64
	Description string
	Label       string
	Required    string
	Accepts     string
	Currency    string
}

type EventGroup struct {
	Id        float64
	Group_Lat float64
	Group_Lon float64
	Name      string
	Join_Mode string
	Urlname   string
	Who       string
}

const MEETUP_API_KEY = "<YOUR_MEETUP_API_KEY>"
const MEETUP_MEMBER_ID = "<YOUR_MEETUP_MEMBER_KEY>"
const TIMEFRAME = "0,1w"

func main() {
	jsonResponse := getEvents()

	showEvents(jsonResponse)
	// log.Printf("%d", time.Parse(jsonResponse.Results[0].Time)
}

func getEvents() MeetupEventCollection {
	client := &http.Client{}

	url := "https://api.meetup.com/2/events?key="+ MEETUP_API_KEY + "&member_id=" + MEETUP_MEMBER_ID + "&time=" + TIMEFRAME
	req, err := http.NewRequest("GET", url, nil)
	resp, err := client.Do(req)
	handleErrorDuring("send request", err)

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	handleErrorDuring("read response", err)

	var jsonResponse MeetupEventCollection
	json.Unmarshal([]byte(body), &jsonResponse)

	return jsonResponse

}

func showEvents(jsonResponse MeetupEventCollection) {
	prettyResponse, err := json.MarshalIndent(jsonResponse, "", "    ")
	handleErrorDuring("marshal response", err)

	os.Stdout.Write(prettyResponse)

}

func handleErrorDuring(context string, err error) {
	if err != nil {
		log.Fatal("Failed to %s: %s", context, err)
	}

}

golang 在go(golang)中使用http.Get的示例

package main

import (
        "fmt"
        "io"
        "log"
        "net/http"
        "os"
)

func main() {
        if len(os.Args) != 2 {
                fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0])
                os.Exit(1)
        }
        response, err := http.Get(os.Args[1])
        if err != nil {
                log.Fatal(err)
        } else {
                defer response.Body.Close()
                _, err := io.Copy(os.Stdout, response.Body)
                if err != nil {
                        log.Fatal(err)
                }
        }
}

以上是关于golang 在Golang中即将举办的Meetup活动的主要内容,如果未能解决你的问题,请参考以下文章

国内 Golang 开发有没有 qian 途?爬了些数据告诉你

一看就懂系列之Golang的测试

golang并发真的比java高吗

Golang最佳实践持续来袭——Gopher Meetup 深圳站

跨语言统一治理Golang,谈谈另辟蹊径的开源RPC框架Motan

golang gc 优化思路以及实例分析