ctx := context.Background() client := model.NewAPIv4Client("https://mattermost.example.com") client.Login("username", "password") params := model.TeamSearch{ Name: "example-team", } teams, resp := client.SearchTeams(params, ctx) if resp.Error != nil { fmt.Println(resp.Error) return } fmt.Printf("ID of team '%s': %s\n", teams[0].Name, teams[0].Id)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() client := model.NewAPIv4Client("https://mattermost.example.com") go func() { channels, resp := client.GetAllChannelsForTeam("example-team", ctx) if resp.Error != nil { fmt.Println(resp.Error) return } fmt.Printf("Number of channels: %d\n", len(channels)) }() select { case <-ctx.Done(): fmt.Println("API request cancelled due to timeout") }In this example, we use the Context T type to cancel a long-running API request if it takes longer than 5 seconds to complete. We set up a client object and make a request to get all channels for a specific team, running it in a separate goroutine. We then use a select statement with the context's Done() channel to wait for either the request to complete or the timeout to occur. If the timeout occurs first, we print a message indicating that the request was cancelled.