{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Go Client for TD-API","description":"Learn how to use the Go programming language client library for Treasure API. List databases and tables, issue queries, and check job status.","siteUrl":"https://docs.treasuredata.com","lang":"en-US","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"go-client-for-td-api","__idx":0},"children":["Go Client for TD-API"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This guide covers how to use the Go programming language module for the td-client API."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"prerequisites","__idx":1},"children":["Prerequisites"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Basic knowledge of Treasure Data, including ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/my-settings/getting-your-api-keys"},"children":["access to the API"," ","key"]},"."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["A table with data."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"installation","__idx":2},"children":["Installation"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Install the td-client via the go command line."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"shell","header":{"controls":{"copy":{}}},"source":"go get github.com/treasure-data/td-client-go\n","lang":"shell"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"setup","__idx":3},"children":["Setup"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Initialize a client object using your Treasure API key. Best practice is to pull the API key from an environment variable."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"go","header":{"controls":{"copy":{}}},"source":"import (\n    td_client \"github.com/treasure-data/td-client-go\"\n    \"time\"\n)\n\nfunc main() {\n    apiKey := os.Getenv(\"TD_API_KEY\")\n    client, err := td_client.NewTDClient(td_client.Settings{\n        ApiKey: apiKey,\n    })\n    if err != nil {\n        fmt.Println(err.Error())\n        return\n    }\n// code to be continued in the rest of this article\n","lang":"go"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"basic-use","__idx":4},"children":["Basic Use"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"list-databases-and-tables","__idx":5},"children":["List Databases and Tables"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The code below demonstrates how to list databases and tables from your Treasure account."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"go","header":{"controls":{"copy":{}}},"source":"databases, err := client.ListDatabases()\nif err != nil {\n    fmt.Println(err.Error())\n    return\n}\nfmt.Printf(\"%d databases\\n\", len(*databases))\nfor _, database := range *databases {\n    fmt.Printf(\"  name: %s\\n\", database.Name)\n    tables, err := client.ListTables(database.Name)\n    if err != nil {\n        fmt.Println(err.Error())\n        return\n    }\n    fmt.Printf(\"  %d tables\\n\", len(*tables))\n    for _, table := range *tables {\n        fmt.Printf(\"    name: %s\\n\", table.Name)\n        fmt.Printf(\"    type: %s\\n\", table.Type)\n        fmt.Printf(\"    count: %d\\n\", table.Count)\n        fmt.Printf(\"    primaryKey: %s\\n\", table.PrimaryKey)\n        fmt.Printf(\"    schema: %v\\n\", table.Schema)\n    }\n}\n","lang":"go"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"issue-queries","__idx":6},"children":["Issue Queries"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The example below issues a query from a Go program. The query API is asynchronous--you can check for query completion by polling the job periodically (e.g. by checking the response for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["JobStatus(jobID)"]}," calls)."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"go","header":{"controls":{"copy":{}}},"source":"jobId, err := client.SubmitQuery(\"sample_db2\", td_client.Query{\n    Type:       \"hive\", // can also use \"trino(presto)\"\n    Query:      \"SELECT COUNT(*) AS c FROM test WHERE a >= 5000\",\n    ResultUrl:  \"\", // can use the Result Output feature\n    Priority:   0,\n    RetryLimit: 0,\n})\nif err != nil {\n    fmt.Println(err.Error())\n    return\n}\nfor {\n    time.Sleep(1000000000)\n    status, err := client.JobStatus(jobId)\n    if err != nil {\n        fmt.Println(err.Error())\n        return\n    }\n    fmt.Printf(\"jobStatus:%s\\n\", status)\n    if status != \"queued\" && status != \"running\" {\n        break\n    }\n}\n","lang":"go"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"get-the-status-of-jobs","__idx":7},"children":["Get the Status of Jobs"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The example below gets the status of a job by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["jobId"]},"."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"go","header":{"controls":{"copy":{}}},"source":"// get job result\njobDesc, err := client.ShowJob(jobId)\nif err != nil {\n    fmt.Println(err.Error())\n    return\n}\nfmt.Printf(\"query:%s\\n\", jobDesc.Query)\nfmt.Printf(\"debug.cmdOut:%s\\n\", jobDesc.Debug.CmdOut)\nfmt.Printf(\"debug.stdErr:%s\\n\", jobDesc.Debug.StdErr)\nfmt.Printf(\"url:%s\\n\", jobDesc.Url)\nfmt.Printf(\"startAt:%s\\n\", jobDesc.StartAt.String())\nfmt.Printf(\"endAt:%s\\n\", jobDesc.EndAt.String())\nfmt.Printf(\"cpuTime:%g\\n\", jobDesc.CpuTime)\nfmt.Printf(\"resultSize:%d\\n\", jobDesc.ResultSize)\nfmt.Printf(\"priority:%d\\n\", jobDesc.Priority)\nfmt.Printf(\"hiveResultSchema:%v\\n\", jobDesc.HiveResultSchema)\n","lang":"go"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"further-reading","__idx":8},"children":["Further Reading"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Source code on ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/treasure-data/td-client-go"},"children":["GitHub"]},"."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/customer-data-platform/data-workbench/queries/hive/quickstart"},"children":["Hive Query Engine"]}]}]}]},"headings":[{"value":"Go Client for TD-API","id":"go-client-for-td-api","depth":1},{"value":"Prerequisites","id":"prerequisites","depth":2},{"value":"Installation","id":"installation","depth":2},{"value":"Setup","id":"setup","depth":2},{"value":"Basic Use","id":"basic-use","depth":2},{"value":"List Databases and Tables","id":"list-databases-and-tables","depth":3},{"value":"Issue Queries","id":"issue-queries","depth":3},{"value":"Get the Status of Jobs","id":"get-the-status-of-jobs","depth":3},{"value":"Further Reading","id":"further-reading","depth":2}],"frontmatter":{"seo":{"title":"Go Client for TD-API","description":"Learn how to use the Go programming language client library for Treasure API. List databases and tables, issue queries, and check job status."}},"lastModified":"2026-06-01T09:09:59.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/apis/td-api/td-client/td-client-go","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}