在 golang 中排序和过滤地图输出 - okta api

我正在通过 go sdk 与 Okta API 进行交互。我是 golang 的新手,能够取回数据,但我不确定如何处理。


func main() {

    client, _ := okta.NewClient(context.Background(), okta.WithOrgUrl({URL}), okta.WithToken({TOKEN}))

    filter := query.NewQueryParams(query.WithFilter("status eq \"ACTIVE\""))

    users, resp, err := client.User.ListUsers(filter)

    fmt.Println(resp, err)

    for _, user := range users {

        fmt.Println(user.Profile)

    }

}

返回:PII 已编辑**


&{0xc000106480} <nil>

&map[email:{email} firstName:{FN} lastName:{LN} login:{login} mobilePhone:<nil> secondEmail:<nil> sshUserName:{ssh}

next user

next user

so on

so on

我的问题是如何将输出过滤到某些字段并进行一些排序或过滤。我有点期待输出是 JSON。


墨色风雨
浏览 111回答 2
2回答

aluckdog

您需要将响应对象编组为 json 对象,您可以漂亮地打印其字符串表示形式。试试这个:import (&nbsp; &nbsp; "context"&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "github.com/okta/okta-sdk-golang/v2/okta"&nbsp; &nbsp; "github.com/okta/okta-sdk-golang/v2/okta/query")var b []bytevar err errorfilter := query.NewQueryParams(query.WithFilter("status eq \"ACTIVE\""))ctx, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl({URL}), okta.WithToken({TOKEN}))users, resp, err := client.User.ListUsers(ctx, filter)if err != nil {&nbsp; &nbsp; log.Println(err.Error())} else {&nbsp; &nbsp; log.Println(resp.Status)&nbsp; &nbsp; if users != nil {&nbsp; &nbsp; &nbsp; &nbsp; b, err = json.MarshalIndent(users, "", " ")&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(string(b))}

梵蒂冈之花

如果您想从 json 输出中隐藏一些结构字段,请遵循以下规则:type User struct {&nbsp; ID int `json:"id"`&nbsp; Username string `json:"username"`&nbsp; MobilePhone string `json:"-"`}您必须使用-字符来隐藏结构字段。我建议您阅读有关 Golang 标签的内容。 https://medium.com/golangspec/tags-in-golang-3e5db0b8ef3e
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go