| 1 |
{ |
| 2 |
"gocloud.dev/blob.ExampleBucket_Delete": { |
| 3 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 4 |
"code": "if err := bucket.Delete(ctx, \"foo.txt\"); err != nil {\n\treturn err\n}" |
| 5 |
}, |
| 6 |
"gocloud.dev/blob.ExampleBucket_NewRangeReader": { |
| 7 |
"imports": "import (\n\t\"context\"\n\t\"io\"\n\t\"os\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 8 |
"code": "// Open the key \"foo.txt\" for reading at offset 1024 and read up to 4096 bytes.\nr, err := bucket.NewRangeReader(ctx, \"foo.txt\", 1024, 4096, nil)\nif err != nil {\n\treturn err\n}\ndefer r.Close()\n// Copy from the read range to stdout.\nif _, err := io.Copy(os.Stdout, r); err != nil {\n\treturn err\n}" |
| 9 |
}, |
| 10 |
"gocloud.dev/blob.ExampleBucket_NewReader": { |
| 11 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 12 |
"code": "// Open the key \"foo.txt\" for reading with the default options.\nr, err := bucket.NewReader(ctx, \"foo.txt\", nil)\nif err != nil {\n\treturn err\n}\ndefer r.Close()\n// Readers also have a limited view of the blob's metadata.\nfmt.Println(\"Content-Type:\", r.ContentType())\nfmt.Println()\n// Copy from the reader to stdout.\nif _, err := io.Copy(os.Stdout, r); err != nil {\n\treturn err\n}" |
| 13 |
}, |
| 14 |
"gocloud.dev/blob.ExampleBucket_NewWriter": { |
| 15 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 16 |
"code": "// Open the key \"foo.txt\" for writing with the default options.\nw, err := bucket.NewWriter(ctx, \"foo.txt\", nil)\nif err != nil {\n\treturn err\n}\n_, writeErr := fmt.Fprintln(w, \"Hello, World!\")\n// Always check the return value of Close when writing.\ncloseErr := w.Close()\nif writeErr != nil {\n\tlog.Fatal(writeErr)\n}\nif closeErr != nil {\n\tlog.Fatal(closeErr)\n}" |
| 17 |
}, |
| 18 |
"gocloud.dev/blob.ExampleBucket_NewWriter_cancel": { |
| 19 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 20 |
"code": "// Create a cancelable context from the existing context.\nwriteCtx, cancelWrite := context.WithCancel(ctx)\ndefer cancelWrite()\n\n// Open the key \"foo.txt\" for writing with the default options.\nw, err := bucket.NewWriter(writeCtx, \"foo.txt\", nil)\nif err != nil {\n\treturn err\n}\n\n// Assume some writes happened and we encountered an error.\n// Now we want to abort the write.\n\nif err != nil {\n\t// First cancel the context.\n\tcancelWrite()\n\t// You must still close the writer to avoid leaking resources.\n\tw.Close()\n}" |
| 21 |
}, |
| 22 |
"gocloud.dev/blob.ExamplePrefixedBucket": { |
| 23 |
"imports": "import \"gocloud.dev/blob\"", |
| 24 |
"code": "// Wrap the bucket using blob.PrefixedBucket.\n// The prefix should end with \"/\", so that the resulting bucket operates\n// in a subfolder.\nbucket = blob.PrefixedBucket(bucket, \"a/subfolder/\")\n\n// The original bucket is no longer usable; it has been closed.\n// The wrapped bucket should be closed when done.\ndefer bucket.Close()\n\n// Bucket operations on \u003ckey\u003e will be translated to \"a/subfolder/\u003ckey\u003e\"." |
| 25 |
}, |
| 26 |
"gocloud.dev/blob.ExampleSingleKeyBucket": { |
| 27 |
"imports": "import \"gocloud.dev/blob\"", |
| 28 |
"code": "// Wrap the bucket using blob.SingleKeyBucket.\n// The bucket always references the provided key.\nbucket = blob.SingleKeyBucket(bucket, \"foo.txt\")\n\n// The original bucket is no longer usable; it has been closed.\n// The wrapped bucket should be closed when done.\ndefer bucket.Close()\n\n// Bucket operations will ignore the passed-in key and always reference foo.txt." |
| 29 |
}, |
| 30 |
"gocloud.dev/blob.Example_openFromURLWithPrefix": { |
| 31 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 32 |
"code": "// Connect to a bucket using a URL, using the \"prefix\" query parameter to\n// target a subfolder in the bucket.\n// The prefix should end with \"/\", so that the resulting bucket operates\n// in a subfolder.\nb, err := blob.OpenBucket(ctx, \"mem://?prefix=a/subfolder/\")\nif err != nil {\n\treturn err\n}\ndefer b.Close()\n\n// Bucket operations on \u003ckey\u003e will be translated to \"a/subfolder/\u003ckey\u003e\"." |
| 33 |
}, |
| 34 |
"gocloud.dev/blob.Example_openFromURLWithSingleKey": { |
| 35 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n)", |
| 36 |
"code": "// Connect to a bucket using a URL, using the \"key\" query parameter to\n// make the bucket always reference that key.\nb, err := blob.OpenBucket(ctx, \"mem://?key=foo.txt\")\nif err != nil {\n\treturn err\n}\ndefer b.Close()\n\n// Bucket operations will ignore the passed-in key and always reference foo.txt." |
| 37 |
}, |
| 38 |
"gocloud.dev/blob/azureblob.ExampleOpenBucket": { |
| 39 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob/azureblob\"\n)", |
| 40 |
"code": "const (\n\t// The storage container to access.\n\tcontainerName = \"my-container\"\n)\n\n// Construct the service URL.\n// There are many forms of service URLs, see ServiceURLOptions.\nopts := azureblob.NewDefaultServiceURLOptions()\nserviceURL, err := azureblob.NewServiceURL(opts)\nif err != nil {\n\treturn err\n}\n\n// There are many ways to authenticate to Azure.\n// This approach uses environment variables as described in azureblob package\n// documentation.\n// For example, to use shared key authentication, you would set\n// AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY.\n// To use a SAS token, you would set AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_SAS_TOKEN.\n// You can also construct a client using the azblob constructors directly, like\n// azblob.NewServiceClientWithSharedKey.\nclient, err := azureblob.NewDefaultClient(serviceURL, containerName)\nif err != nil {\n\treturn err\n}\n\n// Create a *blob.Bucket.\nb, err := azureblob.OpenBucket(ctx, client, nil)\nif err != nil {\n\treturn err\n}\ndefer b.Close()\n\n// Now we can use b to read or write files to the container.\ndata, err := b.ReadAll(ctx, \"my-key\")\nif err != nil {\n\treturn err\n}\n_ = data" |
| 41 |
}, |
| 42 |
"gocloud.dev/blob/azureblob.Example_openBucketFromURL": { |
| 43 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n\t_ \"gocloud.dev/blob/azureblob\"\n)", |
| 44 |
"code": "// blob.OpenBucket creates a *blob.Bucket from a URL.\n// This URL will open the container \"my-container\" using default\n// credentials found in environment variables as documented in\n// the package.\n// Assuming AZURE_STORAGE_ACCOUNT is set to \"myaccount\",\n// and other options aren't set, the service URL will look like:\n// \"https://myaccount.blob.core.windows.net/my-container\".\nbucket, err := blob.OpenBucket(ctx, \"azblob://my-container\")\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()\n\n// Another example, against a local emulator.\n// Assuming AZURE_STORAGE_ACCOUNT is set to \"myaccount\",\n// the service URL will look like:\n// \"http://localhost:10001/myaccount/my-container\".\nlocalbucket, err := blob.OpenBucket(ctx, \"azblob://my-container?protocol=http\u0026domain=localhost:10001\")\nif err != nil {\n\treturn err\n}\ndefer localbucket.Close()" |
| 45 |
}, |
| 46 |
"gocloud.dev/blob/fileblob.ExampleOpenBucket": { |
| 47 |
"imports": "import (\n\t\"os\"\n\n\t\"gocloud.dev/blob/fileblob\"\n)", |
| 48 |
"code": "// The directory you pass to fileblob.OpenBucket must exist first.\nconst myDir = \"path/to/local/directory\"\nif err := os.MkdirAll(myDir, 0o777); err != nil {\n\treturn err\n}\n\n// Create a file-based bucket.\nbucket, err := fileblob.OpenBucket(myDir, nil)\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()" |
| 49 |
}, |
| 50 |
"gocloud.dev/blob/gcsblob.ExampleOpenBucket": { |
| 51 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob/gcsblob\"\n\t\"gocloud.dev/gcp\"\n)", |
| 52 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n\n// Create an HTTP client.\n// This example uses the default HTTP transport and the credentials\n// created above.\nclient, err := gcp.NewHTTPClient(\n\tgcp.DefaultTransport(),\n\tgcp.CredentialsTokenSource(creds))\nif err != nil {\n\treturn err\n}\n\n// Create a *blob.Bucket.\nbucket, err := gcsblob.OpenBucket(ctx, client, \"my-bucket\", nil)\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()" |
| 53 |
}, |
| 54 |
"gocloud.dev/blob/gcsblob.Example_openBucketFromURL": { |
| 55 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n\t_ \"gocloud.dev/blob/gcsblob\"\n)", |
| 56 |
"code": "// blob.OpenBucket creates a *blob.Bucket from a URL.\n// This URL will open the bucket \"my-bucket\" using default credentials.\nbucket, err := blob.OpenBucket(ctx, \"gs://my-bucket\")\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()" |
| 57 |
}, |
| 58 |
"gocloud.dev/blob/memblob.ExampleOpenBucket": { |
| 59 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/blob/memblob\"\n)", |
| 60 |
"code": "// Create an in-memory bucket.\nbucket := memblob.OpenBucket(nil)\ndefer bucket.Close()\n\n// Now we can use bucket to read or write files to the bucket.\nerr := bucket.WriteAll(ctx, \"my-key\", []byte(\"hello world\"), nil)\nif err != nil {\n\treturn err\n}\ndata, err := bucket.ReadAll(ctx, \"my-key\")\nif err != nil {\n\treturn err\n}\nfmt.Println(string(data))\n\n// Output:\n// hello world" |
| 61 |
}, |
| 62 |
"gocloud.dev/blob/s3blob.ExampleOpenBucket": { |
| 63 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/s3\"\n\t\"gocloud.dev/blob/s3blob\"\n)", |
| 64 |
"code": "// Establish a AWS Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Create a *blob.Bucket.\nclient := s3.NewFromConfig(cfg)\nbucket, err := s3blob.OpenBucket(ctx, client, \"my-bucket\", nil)\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()" |
| 65 |
}, |
| 66 |
"gocloud.dev/blob/s3blob.Example_openBucketFromURL": { |
| 67 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/blob\"\n\t_ \"gocloud.dev/blob/s3blob\"\n)", |
| 68 |
"code": "// blob.OpenBucket creates a *blob.Bucket from a URL.\nbucket, err := blob.OpenBucket(ctx, \"s3://my-bucket?region=us-west-1\")\nif err != nil {\n\treturn err\n}\ndefer bucket.Close()" |
| 69 |
}, |
| 70 |
"gocloud.dev/docstore.ExampleCollection_Actions_bulkWrite": { |
| 71 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n)", |
| 72 |
"code": "// Build an ActionList to create several new players, then execute it.\n// The actions may happen in any order.\nnewPlayers := []string{\"Pat\", \"Mel\", \"Fran\"}\nactionList := coll.Actions()\nfor _, p := range newPlayers {\n\tactionList.Create(\u0026Player{Name: p, Score: 0})\n}\nif err := actionList.Do(ctx); err != nil {\n\treturn err\n}" |
| 73 |
}, |
| 74 |
"gocloud.dev/docstore.ExampleCollection_Actions_getAfterWrite": { |
| 75 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/docstore\"\n)", |
| 76 |
"code": "// Add a document to the collection, then retrieve it.\n// Because both the Put and the Get refer to the same document,\n// they happen in order.\ngot := Player{Name: \"Pat\"}\nerr := coll.Actions().Put(\u0026Player{Name: \"Pat\", Score: 88}).Get(\u0026got).Do(ctx)\nif err != nil {\n\treturn err\n}\nfmt.Println(got.Name, got.Score)" |
| 77 |
}, |
| 78 |
"gocloud.dev/docstore.ExampleCollection_Update": { |
| 79 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n)", |
| 80 |
"code": "// Create a player.\npat := \u0026Player{Name: \"Pat\", Score: 0}\nif err := coll.Create(ctx, pat); err != nil {\n\treturn err\n}\n\n// Set the score to a new value.\npat2 := \u0026Player{Name: \"Pat\"}\nerr := coll.Actions().Update(pat, docstore.Mods{\"Score\": 15}).Get(pat2).Do(ctx)\nif err != nil {\n\treturn err\n}\n\n// Increment the score.\nerr = coll.Actions().Update(pat, docstore.Mods{\"Score\": docstore.Increment(5)}).Get(pat2).Do(ctx)\nif err != nil {\n\treturn err\n}" |
| 81 |
}, |
| 82 |
"gocloud.dev/docstore.ExampleQuery_Get": { |
| 83 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\t\"io\"\n\n\t\"gocloud.dev/docstore\"\n)", |
| 84 |
"code": "// Ask for all players with scores at least 20.\niter := coll.Query().Where(\"Score\", \"\u003e=\", 20).OrderBy(\"Score\", docstore.Descending).Get(ctx)\ndefer iter.Stop()\n\n// Query.Get returns an iterator. Call Next on it until io.EOF.\nfor {\n\tvar p Player\n\terr := iter.Next(ctx, \u0026p)\n\tif err == io.EOF {\n\t\tbreak\n\t} else if err != nil {\n\t\treturn err\n\t} else {\n\t\tfmt.Printf(\"%s: %d\\n\", p.Name, p.Score)\n\t}\n}" |
| 85 |
}, |
| 86 |
"gocloud.dev/docstore.Example_optimisticLocking": { |
| 87 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"gocloud.dev/docstore/memdocstore\"\n\t\"gocloud.dev/gcerrors\"\n)", |
| 88 |
"code": "coll, err := memdocstore.OpenCollection(\"Name\", nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()\n\n// Create a player.\npat := \u0026Player{Name: \"Pat\", Score: 7}\nif err := coll.Create(ctx, pat); err != nil {\n\treturn err\n}\nfmt.Println(pat) // memdocstore revisions are deterministic, so we can check the output.\n\n// Double a player's score. We cannot use Update to multiply, so we use optimistic\n// locking instead.\n\n// We may have to retry a few times; put a time limit on that.\nctx, cancel := context.WithTimeout(ctx, 30*time.Second)\ndefer cancel()\nfor {\n\t// Get the document.\n\tplayer := \u0026Player{Name: \"Pat\"}\n\tif err := coll.Get(ctx, player); err != nil {\n\t\treturn err\n\t}\n\t// player.DocstoreRevision is set to the document's revision.\n\n\t// Modify the document locally.\n\tplayer.Score *= 2\n\n\t// Replace the document. player.DocstoreRevision will be checked against\n\t// the stored document's revision.\n\terr := coll.Replace(ctx, player)\n\tif err != nil {\n\t\tcode := gcerrors.Code(err)\n\t\t// On FailedPrecondition or NotFound, try again.\n\t\tif code == gcerrors.FailedPrecondition || code == gcerrors.NotFound {\n\t\t\tcontinue\n\t\t}\n\t\treturn err\n\t}\n\tfmt.Println(player)\n\tbreak\n}\n\n// Output:\n// \u0026{Pat 7 1}\n// \u0026{Pat 14 2}" |
| 89 |
}, |
| 90 |
"gocloud.dev/docstore/awsdynamodb/v2.ExampleOpenCollection": { |
| 91 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/dynamodb\"\n\t\"gocloud.dev/docstore/awsdynamodb/v2\"\n)", |
| 92 |
"code": "cfg, err := config.LoadDefaultConfig(context.Background())\nif err != nil {\n\treturn err\n}\ncoll, err := awsdynamodb.OpenCollection(\n\tdynamodb.NewFromConfig(cfg), \"docstore-test\", \"partitionKeyField\", \"\", nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 93 |
}, |
| 94 |
"gocloud.dev/docstore/awsdynamodb/v2.Example_openCollectionFromURL": { |
| 95 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t_ \"gocloud.dev/docstore/awsdynamodb\"\n)", |
| 96 |
"code": "// docstore.OpenCollection creates a *docstore.Collection from a URL.\ncoll, err := docstore.OpenCollection(ctx, \"dynamodb://my-table?partition_key=name\")\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 97 |
}, |
| 98 |
"gocloud.dev/docstore/gcpfirestore.ExampleOpenCollection": { |
| 99 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore/gcpfirestore\"\n\t\"gocloud.dev/gcp\"\n)", |
| 100 |
"code": "creds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\nclient, _, err := gcpfirestore.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\nresourceID := gcpfirestore.CollectionResourceID(\"my-project\", \"my-collection\")\ncoll, err := gcpfirestore.OpenCollection(client, resourceID, \"userID\", nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 101 |
}, |
| 102 |
"gocloud.dev/docstore/gcpfirestore.ExampleOpenCollectionWithNameFunc": { |
| 103 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t\"gocloud.dev/docstore/gcpfirestore\"\n\t\"gocloud.dev/gcp\"\n)", |
| 104 |
"code": "creds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\nclient, _, err := gcpfirestore.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\n\n// The name of a document is constructed from the Game and Player fields.\nnameFromDocument := func(doc docstore.Document) string {\n\ths := doc.(*HighScore)\n\treturn hs.Game + \"|\" + hs.Player\n}\n\nresourceID := gcpfirestore.CollectionResourceID(\"my-project\", \"my-collection\")\ncoll, err := gcpfirestore.OpenCollectionWithNameFunc(client, resourceID, nameFromDocument, nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 105 |
}, |
| 106 |
"gocloud.dev/docstore/gcpfirestore.Example_openCollectionFromURL": { |
| 107 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t_ \"gocloud.dev/docstore/gcpfirestore\"\n)", |
| 108 |
"code": "// docstore.OpenCollection creates a *docstore.Collection from a URL.\nconst url = \"firestore://projects/my-project/databases/(default)/documents/my-collection?name_field=userID\"\ncoll, err := docstore.OpenCollection(ctx, url)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 109 |
}, |
| 110 |
"gocloud.dev/docstore/memdocstore.ExampleOpenCollection": { |
| 111 |
"imports": "import \"gocloud.dev/docstore/memdocstore\"", |
| 112 |
"code": "coll, err := memdocstore.OpenCollection(\"keyField\", nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 113 |
}, |
| 114 |
"gocloud.dev/docstore/memdocstore.ExampleOpenCollectionWithKeyFunc": { |
| 115 |
"imports": "import (\n\t\"gocloud.dev/docstore\"\n\t\"gocloud.dev/docstore/memdocstore\"\n)", |
| 116 |
"code": "// The name of a document is constructed from the Game and Player fields.\nnameFromDocument := func(doc docstore.Document) any {\n\ths := doc.(*HighScore)\n\treturn hs.Game + \"|\" + hs.Player\n}\n\ncoll, err := memdocstore.OpenCollectionWithKeyFunc(nameFromDocument, nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 117 |
}, |
| 118 |
"gocloud.dev/docstore/memdocstore.Example_openCollectionFromURL": { |
| 119 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t_ \"gocloud.dev/docstore/memdocstore\"\n)", |
| 120 |
"code": "// docstore.OpenCollection creates a *docstore.Collection from a URL.\ncoll, err := docstore.OpenCollection(ctx, \"mem://collection/keyField\")\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 121 |
}, |
| 122 |
"gocloud.dev/docstore/mongodocstore.ExampleOpenCollection": { |
| 123 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore/mongodocstore\"\n)", |
| 124 |
"code": "client, err := mongodocstore.Dial(ctx, \"mongodb://my-host\")\nif err != nil {\n\treturn err\n}\nmcoll := client.Database(\"my-db\").Collection(\"my-coll\")\ncoll, err := mongodocstore.OpenCollection(mcoll, \"userID\", nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 125 |
}, |
| 126 |
"gocloud.dev/docstore/mongodocstore.ExampleOpenCollectionWithIDFunc": { |
| 127 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t\"gocloud.dev/docstore/mongodocstore\"\n)", |
| 128 |
"code": "client, err := mongodocstore.Dial(ctx, \"mongodb://my-host\")\nif err != nil {\n\treturn err\n}\nmcoll := client.Database(\"my-db\").Collection(\"my-coll\")\n\n// The name of a document is constructed from the Game and Player fields.\nnameFromDocument := func(doc docstore.Document) interface{} {\n\ths := doc.(*HighScore)\n\treturn hs.Game + \"|\" + hs.Player\n}\n\ncoll, err := mongodocstore.OpenCollectionWithIDFunc(mcoll, nameFromDocument, nil)\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 129 |
}, |
| 130 |
"gocloud.dev/docstore/mongodocstore.Example_openCollectionFromURL": { |
| 131 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/docstore\"\n\t_ \"gocloud.dev/docstore/mongodocstore\"\n)", |
| 132 |
"code": "// docstore.OpenCollection creates a *docstore.Collection from a URL.\ncoll, err := docstore.OpenCollection(ctx, \"mongo://my-db/my-collection?id_field=userID\")\nif err != nil {\n\treturn err\n}\ndefer coll.Close()" |
| 133 |
}, |
| 134 |
"gocloud.dev/mysql.ExampleOpen": { |
| 135 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/mysql\"\n)", |
| 136 |
"code": "// Replace this with your actual settings.\ndb, err := mysql.Open(ctx, \"mysql://user:password@localhost/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.Exec(\"CREATE TABLE foo (bar INT);\")" |
| 137 |
}, |
| 138 |
"gocloud.dev/mysql/awsmysql.Example": { |
| 139 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/mysql\"\n\t_ \"gocloud.dev/mysql/awsmysql\"\n)", |
| 140 |
"code": "// Replace these with your actual settings.\ndb, err := mysql.Open(ctx,\n\t\"awsmysql://myrole:[email protected]/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.ExecContext(ctx, \"CREATE TABLE foo (bar INT);\")" |
| 141 |
}, |
| 142 |
"gocloud.dev/mysql/azuremysql.Example": { |
| 143 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/mysql\"\n\t_ \"gocloud.dev/mysql/azuremysql\"\n)", |
| 144 |
"code": "// Replace this with your actual settings.\ndb, err := mysql.Open(ctx,\n\t\"azuremysql://user:[email protected]/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.Exec(\"CREATE TABLE foo (bar INT);\")" |
| 145 |
}, |
| 146 |
"gocloud.dev/mysql/gcpmysql.Example": { |
| 147 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/mysql\"\n\t_ \"gocloud.dev/mysql/gcpmysql\"\n)", |
| 148 |
"code": "// Replace this with your actual settings.\ndb, err := mysql.Open(ctx,\n\t\"gcpmysql://user:password@example-project/region/my-instance01/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.Exec(\"CREATE TABLE foo (bar INT);\")" |
| 149 |
}, |
| 150 |
"gocloud.dev/postgres.ExampleOpen": { |
| 151 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/postgres\"\n)", |
| 152 |
"code": "// Replace this with your actual settings.\ndb, err := postgres.Open(ctx, \"postgres://user:password@localhost/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.Exec(\"CREATE TABLE foo (bar INT);\")" |
| 153 |
}, |
| 154 |
"gocloud.dev/postgres/awspostgres.Example": { |
| 155 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/postgres\"\n\t_ \"gocloud.dev/postgres/awspostgres\"\n)", |
| 156 |
"code": "// Replace these with your actual settings.\ndb, err := postgres.Open(ctx,\n\t\"awspostgres://myrole:[email protected]/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.ExecContext(ctx, \"CREATE TABLE foo (bar INT);\")" |
| 157 |
}, |
| 158 |
"gocloud.dev/postgres/gcppostgres.Example": { |
| 159 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/postgres\"\n\t_ \"gocloud.dev/postgres/gcppostgres\"\n)", |
| 160 |
"code": "// Replace this with your actual settings.\ndb, err := postgres.Open(ctx,\n\t\"gcppostgres://user:password@example-project/region/my-instance01/testdb\")\nif err != nil {\n\treturn err\n}\ndefer db.Close()\n\n// Use database in your program.\ndb.Exec(\"CREATE TABLE foo (bar INT);\")" |
| 161 |
}, |
| 162 |
"gocloud.dev/pubsub.ExampleSubscription_Receive": { |
| 163 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/pubsub\"\n)", |
| 164 |
"code": "// Loop on received messages.\nfor {\n\tmsg, err := subscription.Receive(ctx)\n\tif err != nil {\n\t\t// Errors from Receive indicate that Receive will no longer succeed.\n\t\tlog.Printf(\"Receiving message: %v\", err)\n\t\tbreak\n\t}\n\t// Do work based on the message, for example:\n\tfmt.Printf(\"Got message: %q\\n\", msg.Body)\n\t// Messages must always be acknowledged with Ack.\n\tmsg.Ack()\n}" |
| 165 |
}, |
| 166 |
"gocloud.dev/pubsub.ExampleSubscription_Receive_concurrent": { |
| 167 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/pubsub\"\n)", |
| 168 |
"code": "// Loop on received messages. We can use a channel as a semaphore to limit how\n// many goroutines we have active at a time as well as wait on the goroutines\n// to finish before exiting.\nconst maxHandlers = 10\nsem := make(chan struct{}, maxHandlers)\nrecvLoop:\nfor {\n\tmsg, err := subscription.Receive(ctx)\n\tif err != nil {\n\t\t// Errors from Receive indicate that Receive will no longer succeed.\n\t\tlog.Printf(\"Receiving message: %v\", err)\n\t\tbreak\n\t}\n\n\t// Wait if there are too many active handle goroutines and acquire the\n\t// semaphore. If the context is canceled, stop waiting and start shutting\n\t// down.\n\tselect {\n\tcase sem \u003c- struct{}{}:\n\tcase \u003c-ctx.Done():\n\t\tbreak recvLoop\n\t}\n\n\t// Handle the message in a new goroutine.\n\tgo func() {\n\t\tdefer func() { \u003c-sem }() // Release the semaphore.\n\t\tdefer msg.Ack() // Messages must always be acknowledged with Ack.\n\n\t\t// Do work based on the message, for example:\n\t\tfmt.Printf(\"Got message: %q\\n\", msg.Body)\n\t}()\n}\n\n// We're no longer receiving messages. Wait to finish handling any\n// unacknowledged messages by totally acquiring the semaphore.\nfor range maxHandlers {\n\tsem \u003c- struct{}{}\n}" |
| 169 |
}, |
| 170 |
"gocloud.dev/pubsub.ExampleTopic_Send": { |
| 171 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n)", |
| 172 |
"code": "err := topic.Send(ctx, \u0026pubsub.Message{\n\tBody: []byte(\"Hello, World!\\n\"),\n\t// Metadata is optional and can be nil.\n\tMetadata: map[string]string{\n\t\t// These are examples of metadata.\n\t\t// There is nothing special about the key names.\n\t\t\"language\": \"en\",\n\t\t\"importance\": \"high\",\n\t},\n})\nif err != nil {\n\treturn err\n}" |
| 173 |
}, |
| 174 |
"gocloud.dev/pubsub/awssnssqs.ExampleOpenSNSTopic": { |
| 175 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/sns\"\n\t\"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 176 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Create a *pubsub.Topic.\nconst topicARN = \"arn:aws:sns:us-east-2:123456789012:mytopic\"\nclient := sns.NewFromConfig(cfg)\ntopic := awssnssqs.OpenSNSTopic(ctx, client, topicARN, nil)\ndefer topic.Shutdown(ctx)" |
| 177 |
}, |
| 178 |
"gocloud.dev/pubsub/awssnssqs.ExampleOpenSQSTopic": { |
| 179 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/sqs\"\n\t\"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 180 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Create a *pubsub.Topic.\nconst queueURL = \"https://sqs.us-east-2.amazonaws.com/123456789012/myqueue\"\nclient := sqs.NewFromConfig(cfg)\ntopic := awssnssqs.OpenSQSTopic(ctx, client, queueURL, nil)\ndefer topic.Shutdown(ctx)" |
| 181 |
}, |
| 182 |
"gocloud.dev/pubsub/awssnssqs.ExampleOpenSubscription": { |
| 183 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/sqs\"\n\t\"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 184 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Construct a *pubsub.Subscription.\n// https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/QueueURL.html\nconst queueURL = \"https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue\"\nclient := sqs.NewFromConfig(cfg)\nsubscription := awssnssqs.OpenSubscription(ctx, client, queueURL, nil)\ndefer subscription.Shutdown(ctx)" |
| 185 |
}, |
| 186 |
"gocloud.dev/pubsub/awssnssqs.Example_openSNSTopicFromURL": { |
| 187 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 188 |
"code": "const topicARN = \"arn:aws:sns:us-east-2:123456789012:mytopic\"\n// Note the 3 slashes; ARNs have multiple colons and therefore aren't valid\n// as hostnames in the URL.\ntopic, err := pubsub.OpenTopic(ctx, \"awssns:///\"+topicARN+\"?region=us-east-2\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 189 |
}, |
| 190 |
"gocloud.dev/pubsub/awssnssqs.Example_openSQSTopicFromURL": { |
| 191 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 192 |
"code": "// https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/QueueURL.html\nconst queueURL = \"sqs.us-east-2.amazonaws.com/123456789012/myqueue\"\ntopic, err := pubsub.OpenTopic(ctx, \"awssqs://\"+queueURL+\"?region=us-east-2\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 193 |
}, |
| 194 |
"gocloud.dev/pubsub/awssnssqs.Example_openSubscriptionFromURL": { |
| 195 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/awssnssqs\"\n)", |
| 196 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will open the subscription with the URL\n// \"https://sqs.us-east-2.amazonaws.com/123456789012/myqueue\".\nsubscription, err := pubsub.OpenSubscription(ctx,\n\t\"awssqs://sqs.us-east-2.amazonaws.com/123456789012/\"+\n\t\t\"myqueue?region=us-east-2\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 197 |
}, |
| 198 |
"gocloud.dev/pubsub/azuresb.ExampleOpenSubscription": { |
| 199 |
"imports": "import (\n\t\"context\"\n\t\"os\"\n\n\t\"gocloud.dev/pubsub/azuresb\"\n)", |
| 200 |
"code": "// Change these as needed for your application.\nserviceBusConnString := os.Getenv(\"SERVICEBUS_CONNECTION_STRING\")\nconst topicName = \"test-topic\"\nconst subscriptionName = \"test-subscription\"\n\n// Connect to Azure Service Bus for the given subscription.\nsbClient, err := azuresb.NewClientFromConnectionString(serviceBusConnString, nil)\nif err != nil {\n\treturn err\n}\nsbReceiver, err := azuresb.NewReceiver(sbClient, topicName, subscriptionName, nil)\nif err != nil {\n\treturn err\n}\ndefer sbReceiver.Close(ctx)\n\n// Construct a *pubsub.Subscription.\nsubscription, err := azuresb.OpenSubscription(ctx, sbClient, sbReceiver, nil)\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 201 |
}, |
| 202 |
"gocloud.dev/pubsub/azuresb.ExampleOpenTopic": { |
| 203 |
"imports": "import (\n\t\"context\"\n\t\"os\"\n\n\t\"gocloud.dev/pubsub/azuresb\"\n)", |
| 204 |
"code": "// Change these as needed for your application.\nconnString := os.Getenv(\"SERVICEBUS_CONNECTION_STRING\")\ntopicName := \"test-topic\"\n\nif connString == \"\" {\n\tlog.Fatal(\"Service Bus ConnectionString is not set\")\n}\n\n// Connect to Azure Service Bus for the given topic.\nsbClient, err := azuresb.NewClientFromConnectionString(connString, nil)\nif err != nil {\n\treturn err\n}\nsbSender, err := azuresb.NewSender(sbClient, topicName, nil)\nif err != nil {\n\treturn err\n}\ndefer sbSender.Close(ctx)\n\n// Construct a *pubsub.Topic.\ntopic, err := azuresb.OpenTopic(ctx, sbSender, nil)\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 205 |
}, |
| 206 |
"gocloud.dev/pubsub/azuresb.Example_openSubscriptionFromURL": { |
| 207 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/azuresb\"\n)", |
| 208 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will open the subscription \"mysubscription\" for the topic\n// \"mytopic\" using a connection string from the environment variable\n// SERVICEBUS_CONNECTION_STRING.\nsubscription, err := pubsub.OpenSubscription(ctx,\n\t\"azuresb://mytopic?subscription=mysubscription\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 209 |
}, |
| 210 |
"gocloud.dev/pubsub/azuresb.Example_openTopicFromURL": { |
| 211 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/azuresb\"\n)", |
| 212 |
"code": "// pubsub.OpenTopic creates a *pubsub.Topic from a URL.\n// This URL will open the topic \"mytopic\" using a connection string\n// from the environment variable SERVICEBUS_CONNECTION_STRING.\ntopic, err := pubsub.OpenTopic(ctx, \"azuresb://mytopic\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 213 |
}, |
| 214 |
"gocloud.dev/pubsub/gcppubsub.ExampleOpenSubscription": { |
| 215 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/pubsub/gcppubsub\"\n)", |
| 216 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n\n// Open a gRPC connection to the GCP Pub/Sub API.\nconn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// Construct a SubscriberClient using the connection.\nsubClient, err := gcppubsub.SubscriberClient(ctx, conn)\nif err != nil {\n\treturn err\n}\ndefer subClient.Close()\n\n// Construct a *pubsub.Subscription.\nsubscription, err := gcppubsub.OpenSubscriptionByPath(\n\tsubClient, \"projects/myprojectID/subscriptions/example-subscription\", nil)\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 217 |
}, |
| 218 |
"gocloud.dev/pubsub/gcppubsub.ExampleOpenTopic": { |
| 219 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/pubsub/gcppubsub\"\n)", |
| 220 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n// Open a gRPC connection to the GCP Pub/Sub API.\nconn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// Construct a PublisherClient using the connection.\npubClient, err := gcppubsub.PublisherClient(ctx, conn)\nif err != nil {\n\treturn err\n}\ndefer pubClient.Close()\n\n// Construct a *pubsub.Topic.\ntopic, err := gcppubsub.OpenTopicByPath(pubClient, \"projects/myprojectID/topics/example-topic\", nil)\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 221 |
}, |
| 222 |
"gocloud.dev/pubsub/gcppubsub.Example_openSubscriptionFromURL": { |
| 223 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/gcppubsub\"\n)", |
| 224 |
"code": "subscription, err := pubsub.OpenSubscription(ctx,\n\t\"gcppubsub://projects/my-project/subscriptions/my-subscription\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 225 |
}, |
| 226 |
"gocloud.dev/pubsub/gcppubsub.Example_openTopicFromURL": { |
| 227 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/gcppubsub\"\n)", |
| 228 |
"code": "topic, err := pubsub.OpenTopic(ctx, \"gcppubsub://projects/myproject/topics/mytopic\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 229 |
}, |
| 230 |
"gocloud.dev/pubsub/gcppubsubv2.ExampleOpenSubscription": { |
| 231 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/pubsub/gcppubsubv2\"\n)", |
| 232 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n\n// Open a gRPC connection to the GCP Pub/Sub API.\n// Alternatively, skip this and pass nil to Client below to use the default.\nconn, cleanup, err := gcppubsubv2.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// Construct a Client using the connection.\nclient, err := gcppubsubv2.Client(ctx, \"myprojectID\", conn)\nif err != nil {\n\treturn err\n}\ndefer client.Close()\n\n// Construct a *pubsub.Subscription.\nsubscription := gcppubsubv2.OpenSubscription(client, \"example-subscription\", nil)\ndefer subscription.Shutdown(ctx)" |
| 233 |
}, |
| 234 |
"gocloud.dev/pubsub/gcppubsubv2.ExampleOpenTopic": { |
| 235 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/pubsub/gcppubsubv2\"\n)", |
| 236 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n// Open a gRPC connection to the GCP Pub/Sub API.\n// Alternatively, skip this and pass nil to Client below to use the default.\nconn, cleanup, err := gcppubsubv2.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// Construct a Client using the connection.\nclient, err := gcppubsubv2.Client(ctx, \"myprojectid\", conn)\nif err != nil {\n\treturn err\n}\ndefer client.Close()\n\n// Construct a *pubsub.Topic.\ntopic := gcppubsubv2.OpenTopic(client, \"example-topic\", nil)\ndefer topic.Shutdown(ctx)" |
| 237 |
}, |
| 238 |
"gocloud.dev/pubsub/gcppubsubv2.Example_openSubscriptionFromURL": { |
| 239 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/gcppubsubv2\"\n)", |
| 240 |
"code": "subscription, err := pubsub.OpenSubscription(ctx,\n\t\"gcppubsubv2://projects/my-project/subscriptions/my-subscription\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 241 |
}, |
| 242 |
"gocloud.dev/pubsub/gcppubsubv2.Example_openTopicFromURL": { |
| 243 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/gcppubsubv2\"\n)", |
| 244 |
"code": "topic, err := pubsub.OpenTopic(ctx, \"gcppubsubv2://projects/myproject/topics/mytopic\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 245 |
}, |
| 246 |
"gocloud.dev/pubsub/kafkapubsub.ExampleOpenSubscription": { |
| 247 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub/kafkapubsub\"\n)", |
| 248 |
"code": "// The set of brokers in the Kafka cluster.\naddrs := []string{\"1.2.3.4:9092\"}\n// The Kafka client configuration to use.\nconfig := kafkapubsub.MinimalConfig()\n\n// Construct a *pubsub.Subscription, joining the consumer group \"my-group\"\n// and receiving messages from \"my-topic\".\nsubscription, err := kafkapubsub.OpenSubscription(\n\taddrs, config, \"my-group\", []string{\"my-topic\"}, nil)\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 249 |
}, |
| 250 |
"gocloud.dev/pubsub/kafkapubsub.ExampleOpenTopic": { |
| 251 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub/kafkapubsub\"\n)", |
| 252 |
"code": "// The set of brokers in the Kafka cluster.\naddrs := []string{\"1.2.3.4:9092\"}\n// The Kafka client configuration to use.\nconfig := kafkapubsub.MinimalConfig()\n\n// Construct a *pubsub.Topic.\ntopic, err := kafkapubsub.OpenTopic(addrs, config, \"my-topic\", nil)\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 253 |
}, |
| 254 |
"gocloud.dev/pubsub/kafkapubsub.Example_openSubscriptionFromURL": { |
| 255 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/kafkapubsub\"\n)", |
| 256 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// The host + path are used as the consumer group name.\n// The \"topic\" query parameter sets one or more topics to subscribe to.\n// The set of brokers must be in an environment variable KAFKA_BROKERS.\nsubscription, err := pubsub.OpenSubscription(ctx,\n\t\"kafka://my-group?topic=my-topic\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 257 |
}, |
| 258 |
"gocloud.dev/pubsub/kafkapubsub.Example_openTopicFromURL": { |
| 259 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/kafkapubsub\"\n)", |
| 260 |
"code": "// pubsub.OpenTopic creates a *pubsub.Topic from a URL.\n// The host + path are the topic name to send to.\n// The set of brokers must be in an environment variable KAFKA_BROKERS.\ntopic, err := pubsub.OpenTopic(ctx, \"kafka://my-topic?key_name=x-partition-key\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 261 |
}, |
| 262 |
"gocloud.dev/pubsub/mempubsub.ExampleNewSubscription": { |
| 263 |
"imports": "import (\n\t\"context\"\n\t\"time\"\n\n\t\"gocloud.dev/pubsub/mempubsub\"\n)", |
| 264 |
"code": "// Construct a *pubsub.Topic.\ntopic := mempubsub.NewTopic()\ndefer topic.Shutdown(ctx)\n\n// Construct a *pubsub.Subscription for the topic.\nsubscription := mempubsub.NewSubscription(topic, 1*time.Minute /* ack deadline */)\ndefer subscription.Shutdown(ctx)" |
| 265 |
}, |
| 266 |
"gocloud.dev/pubsub/mempubsub.ExampleNewTopic": { |
| 267 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub/mempubsub\"\n)", |
| 268 |
"code": "topic := mempubsub.NewTopic()\ndefer topic.Shutdown(ctx)" |
| 269 |
}, |
| 270 |
"gocloud.dev/pubsub/mempubsub.Example_openSubscriptionFromURL": { |
| 271 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/mempubsub\"\n)", |
| 272 |
"code": "// Create a topic.\ntopic, err := pubsub.OpenTopic(ctx, \"mem://topicA\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)\n\n// Create a subscription connected to that topic.\nsubscription, err := pubsub.OpenSubscription(ctx, \"mem://topicA\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 273 |
}, |
| 274 |
"gocloud.dev/pubsub/mempubsub.Example_openTopicFromURL": { |
| 275 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/mempubsub\"\n)", |
| 276 |
"code": "topic, err := pubsub.OpenTopic(ctx, \"mem://topicA\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 277 |
}, |
| 278 |
"gocloud.dev/pubsub/natspubsub.ExampleOpenSubscription": { |
| 279 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/nats-io/nats.go\"\n\t\"gocloud.dev/pubsub/natspubsub\"\n)", |
| 280 |
"code": "natsConn, err := nats.Connect(\"nats://nats.example.com\")\nif err != nil {\n\treturn err\n}\ndefer natsConn.Close()\n\nsubscription, err := natspubsub.OpenSubscription(\n\tnatsConn,\n\t\"example.mysubject\",\n\tnil)\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 281 |
}, |
| 282 |
"gocloud.dev/pubsub/natspubsub.ExampleOpenSubscriptionV2": { |
| 283 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/nats-io/nats.go\"\n\t\"gocloud.dev/pubsub/natspubsub\"\n)", |
| 284 |
"code": "subscription, err := natspubsub.OpenSubscriptionV2(\n\tnatsConn,\n\t\"example.mysubject\",\n\tnil)\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 285 |
}, |
| 286 |
"gocloud.dev/pubsub/natspubsub.ExampleOpenSubscription_queue": { |
| 287 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/nats-io/nats.go\"\n\t\"gocloud.dev/pubsub/natspubsub\"\n)", |
| 288 |
"code": "natsConn, err := nats.Connect(\"nats://nats.example.com\")\nif err != nil {\n\treturn err\n}\ndefer natsConn.Close()\n\nsubscription, err := natspubsub.OpenSubscription(\n\tnatsConn,\n\t\"example.mysubject\",\n\t\u0026natspubsub.SubscriptionOptions{Queue: \"queue1\"})\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 289 |
}, |
| 290 |
"gocloud.dev/pubsub/natspubsub.ExampleOpenTopic": { |
| 291 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/nats-io/nats.go\"\n\t\"gocloud.dev/pubsub/natspubsub\"\n)", |
| 292 |
"code": "natsConn, err := nats.Connect(\"nats://nats.example.com\")\nif err != nil {\n\treturn err\n}\ndefer natsConn.Close()\n\ntopic, err := natspubsub.OpenTopic(natsConn, \"example.mysubject\", nil)\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 293 |
}, |
| 294 |
"gocloud.dev/pubsub/natspubsub.ExampleOpenTopicV2": { |
| 295 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/nats-io/nats.go\"\n\t\"gocloud.dev/pubsub/natspubsub\"\n)", |
| 296 |
"code": "natsConn, err := nats.Connect(\"nats://nats.example.com\")\nif err != nil {\n\treturn err\n}\ndefer natsConn.Close()\n\ntopic, err := natspubsub.OpenTopicV2(natsConn, \"example.mysubject\", nil)\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 297 |
}, |
| 298 |
"gocloud.dev/pubsub/natspubsub.Example_openQueueSubscriptionFromURL": { |
| 299 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/natspubsub\"\n)", |
| 300 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will Dial the NATS server at the URL in the environment variable\n// NATS_SERVER_URL and receive messages with subject \"example.mysubject\"\n// This URL will be parsed and the queue attribute will be used as the Queue parameter when creating the NATS Subscription.\nsubscription, err := pubsub.OpenSubscription(ctx, \"nats://example.mysubject?queue=myqueue\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 301 |
}, |
| 302 |
"gocloud.dev/pubsub/natspubsub.Example_openSubscriptionFromURL": { |
| 303 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/natspubsub\"\n)", |
| 304 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will Dial the NATS server at the URL in the environment variable\n// NATS_SERVER_URL and receive messages with subject \"example.mysubject\".\nsubscription, err := pubsub.OpenSubscription(ctx, \"nats://example.mysubject\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 305 |
}, |
| 306 |
"gocloud.dev/pubsub/natspubsub.Example_openSubscriptionV2FromURL": { |
| 307 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/natspubsub\"\n)", |
| 308 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will Dial the NATS server at the URL in the environment variable\n// NATS_SERVER_URL and receive messages with subject \"example.mysubject\".\n// This URL will be parsed and the natsv2 attribute will be used to\n// use NATS v2.2.0+ native message headers as the message metadata.\nsubscription, err := pubsub.OpenSubscription(ctx, \"nats://example.mysubject?natsv2\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 309 |
}, |
| 310 |
"gocloud.dev/pubsub/natspubsub.Example_openTopicFromURL": { |
| 311 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/natspubsub\"\n)", |
| 312 |
"code": "// pubsub.OpenTopic creates a *pubsub.Topic from a URL.\n// This URL will Dial the NATS server at the URL in the environment variable\n// NATS_SERVER_URL and send messages with subject \"example.mysubject\".\ntopic, err := pubsub.OpenTopic(ctx, \"nats://example.mysubject\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 313 |
}, |
| 314 |
"gocloud.dev/pubsub/natspubsub.Example_openTopicV2FromURL": { |
| 315 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/natspubsub\"\n)", |
| 316 |
"code": "// pubsub.OpenTopic creates a *pubsub.Topic from a URL.\n// This URL will Dial the NATS server at the URL in the environment variable\n// NATS_SERVER_URL and send messages with subject \"example.mysubject\".\n// This URL will be parsed and the natsv2 attribute will be used to\n// use NATS v2.2.0+ native message headers as the message metadata.\ntopic, err := pubsub.OpenTopic(ctx, \"nats://example.mysubject?natsv2\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 317 |
}, |
| 318 |
"gocloud.dev/pubsub/rabbitpubsub.ExampleOpenSubscription": { |
| 319 |
"imports": "import (\n\t\"context\"\n\n\tamqp \"github.com/rabbitmq/amqp091-go\"\n\t\"gocloud.dev/pubsub/rabbitpubsub\"\n)", |
| 320 |
"code": "rabbitConn, err := amqp.Dial(\"amqp://guest:guest@localhost:5672/\")\nif err != nil {\n\treturn err\n}\ndefer rabbitConn.Close()\nsubscription := rabbitpubsub.OpenSubscription(rabbitConn, \"myqueue\", nil)\ndefer subscription.Shutdown(ctx)" |
| 321 |
}, |
| 322 |
"gocloud.dev/pubsub/rabbitpubsub.ExampleOpenTopic": { |
| 323 |
"imports": "import (\n\t\"context\"\n\n\tamqp \"github.com/rabbitmq/amqp091-go\"\n\t\"gocloud.dev/pubsub/rabbitpubsub\"\n)", |
| 324 |
"code": "rabbitConn, err := amqp.Dial(\"amqp://guest:guest@localhost:5672/\")\nif err != nil {\n\treturn err\n}\ndefer rabbitConn.Close()\ntopic := rabbitpubsub.OpenTopic(rabbitConn, \"myexchange\", nil)\ndefer topic.Shutdown(ctx)" |
| 325 |
}, |
| 326 |
"gocloud.dev/pubsub/rabbitpubsub.Example_openSubscriptionFromURL": { |
| 327 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/rabbitpubsub\"\n)", |
| 328 |
"code": "// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.\n// This URL will Dial the RabbitMQ server at the URL in the environment\n// variable RABBIT_SERVER_URL and open the queue \"myqueue\".\nsubscription, err := pubsub.OpenSubscription(ctx, \"rabbit://myqueue\")\nif err != nil {\n\treturn err\n}\ndefer subscription.Shutdown(ctx)" |
| 329 |
}, |
| 330 |
"gocloud.dev/pubsub/rabbitpubsub.Example_openTopicFromURL": { |
| 331 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/pubsub\"\n\t_ \"gocloud.dev/pubsub/rabbitpubsub\"\n)", |
| 332 |
"code": "// pubsub.OpenTopic creates a *pubsub.Topic from a URL.\n// This URL will Dial the RabbitMQ server at the URL in the environment\n// variable RABBIT_SERVER_URL and open the exchange \"myexchange\".\ntopic, err := pubsub.OpenTopic(ctx, \"rabbit://myexchange\")\nif err != nil {\n\treturn err\n}\ndefer topic.Shutdown(ctx)" |
| 333 |
}, |
| 334 |
"gocloud.dev/runtimevar.ExampleDecryptDecode": { |
| 335 |
"imports": "import (\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/secrets\"\n)", |
| 336 |
"code": "decodeFunc := runtimevar.DecryptDecode(keeper, runtimevar.StringDecode)\ndecoder := runtimevar.NewDecoder(\"\", decodeFunc)" |
| 337 |
}, |
| 338 |
"gocloud.dev/runtimevar.ExampleVariable_Latest": { |
| 339 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n)", |
| 340 |
"code": "snapshot, err := v.Latest(context.Background())\nif err != nil {\n\tlog.Fatalf(\"Error in retrieving variable: %v\", err)\n}" |
| 341 |
}, |
| 342 |
"gocloud.dev/runtimevar.Example_jsonDecoder": { |
| 343 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/constantvar\"\n)", |
| 344 |
"code": "// Config is the sample config struct we're going to parse our JSON into.\ntype Config struct {\n\tHost string\n\tPort int\n}\n\n// A sample JSON config that will decode into Config.\nconst jsonConfig = `{\"Host\": \"gocloud.dev\", \"Port\": 8080}`\n\n// Construct a Decoder that decodes raw bytes into our config.\ndecoder := runtimevar.NewDecoder(Config{}, runtimevar.JSONDecode)\n\n// Next, a construct a *Variable using a constructor or URL opener.\n// This example uses constantvar.\n// If you're using a URL opener, you can't decode JSON into a struct, but\n// you can use the query parameter \"decoder=jsonmap\" to decode into a map.\nv := constantvar.NewBytes([]byte(jsonConfig), decoder)\ndefer v.Close()\n// snapshot.Value will be of type Config." |
| 345 |
}, |
| 346 |
"gocloud.dev/runtimevar/awsparamstore.ExampleOpenVariable": { |
| 347 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/ssm\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/awsparamstore\"\n)", |
| 348 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\nctx := context.Background()\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Construct a *runtimevar.Variable that watches the variable.\nclient := ssm.NewFromConfig(cfg)\nv, err := awsparamstore.OpenVariable(client, \"cfg-variable-name\", runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 349 |
}, |
| 350 |
"gocloud.dev/runtimevar/awsparamstore.Example_openVariableFromURL": { |
| 351 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/awsparamstore\"\n)", |
| 352 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\nv, err := runtimevar.OpenVariable(ctx, \"awsparamstore://myvar?region=us-west-1\u0026decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 353 |
}, |
| 354 |
"gocloud.dev/runtimevar/awssecretsmanager.ExampleOpenVariable": { |
| 355 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"github.com/aws/aws-sdk-go-v2/service/secretsmanager\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/awssecretsmanager\"\n)", |
| 356 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\nctx := context.Background()\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Construct a *runtimevar.Variable that watches the variable.\n// `secret-variable-name` must be a friendly name of the secret, NOT the Amazon Resource Name (ARN).\nclient := secretsmanager.NewFromConfig(cfg)\nv, err := awssecretsmanager.OpenVariable(client, \"secret-variable-name\", runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 357 |
}, |
| 358 |
"gocloud.dev/runtimevar/awssecretsmanager.Example_openVariableFromURL": { |
| 359 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/awssecretsmanager\"\n)", |
| 360 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// `secret-variable-name` must be a friendly name of the secret, NOT the Amazon Resource Name (ARN).\nv, err := runtimevar.OpenVariable(ctx, \"awssecretsmanager://secret-variable-name?region=us-east-2\u0026decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 361 |
}, |
| 362 |
"gocloud.dev/runtimevar/blobvar.Example_openVariableFromURL": { |
| 363 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/blobvar\"\n)", |
| 364 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The default opener opens a blob.Bucket via a URL, based on the environment\n// variable BLOBVAR_BUCKET_URL.\n\nv, err := runtimevar.OpenVariable(ctx, \"blob://myvar.txt?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 365 |
}, |
| 366 |
"gocloud.dev/runtimevar/constantvar.Example_openVariableFromURL": { |
| 367 |
"imports": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/constantvar\"\n)", |
| 368 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The constant value is in the URL param \"val\".\nv, err := runtimevar.OpenVariable(ctx, \"constant://?val=hello+world\u0026decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()\n\n// The constant value is read from an environment variable specified in \"envvar\".\nv2, err := runtimevar.OpenVariable(ctx, \"constant://?envvar=MY_ENVIRONMENT_VARIABLE\u0026decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v2.Close()" |
| 369 |
}, |
| 370 |
"gocloud.dev/runtimevar/etcdvar.ExampleOpenVariable": { |
| 371 |
"imports": "import (\n\t\"go.etcd.io/etcd/client/v3\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/etcdvar\"\n)", |
| 372 |
"code": "// Connect to the etcd server.\nclient, err := clientv3.NewFromURL(\"http://your.etcd.server:9999\")\nif err != nil {\n\treturn err\n}\n\n// Construct a *runtimevar.Variable that watches the variable.\nv, err := etcdvar.OpenVariable(client, \"cfg-variable-name\", runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 373 |
}, |
| 374 |
"gocloud.dev/runtimevar/etcdvar.Example_openVariableFromURL": { |
| 375 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/etcdvar\"\n)", |
| 376 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The default opener connects to an etcd server based on the environment\n// variable ETCD_SERVER_URL.\n\nv, err := runtimevar.OpenVariable(ctx, \"etcd://myvarname?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 377 |
}, |
| 378 |
"gocloud.dev/runtimevar/filevar.Example_openVariableFromURL": { |
| 379 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/filevar\"\n)", |
| 380 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n\nv, err := runtimevar.OpenVariable(ctx, \"file:///path/to/config.txt?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 381 |
}, |
| 382 |
"gocloud.dev/runtimevar/gcpruntimeconfig.ExampleOpenVariable": { |
| 383 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/gcpruntimeconfig\"\n)", |
| 384 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n\n// Connect to the Runtime Configurator service.\nclient, cleanup, err := gcpruntimeconfig.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// You can use the VariableKey helper to construct a Variable key from\n// your project ID, config ID, and the variable name; alternatively,\n// you can construct the full string yourself (e.g.,\n// \"projects/gcp-project-id/configs/config-id/variables/variable-name\").\n// See https://cloud.google.com/deployment-manager/runtime-configurator/\n// for more details.\n//\n// For this example, the GCP Cloud Runtime Configurator variable being\n// referenced should have a JSON string that decodes into MyConfig.\nvariableKey := gcpruntimeconfig.VariableKey(\"gcp-project-id\", \"config-id\", \"variable-name\")\n\n// Construct a *runtimevar.Variable that watches the variable.\nv, err := gcpruntimeconfig.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 385 |
}, |
| 386 |
"gocloud.dev/runtimevar/gcpruntimeconfig.Example_openVariableFromURL": { |
| 387 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/gcpruntimeconfig\"\n)", |
| 388 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The URL Host+Path are used as the GCP Runtime Configurator Variable key;\n// see https://cloud.google.com/deployment-manager/runtime-configurator/\n// for more details.\n\nv, err := runtimevar.OpenVariable(ctx, \"gcpruntimeconfig://projects/myproject/configs/myconfigid/variables/myvar?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 389 |
}, |
| 390 |
"gocloud.dev/runtimevar/gcpsecretmanager.ExampleOpenVariable": { |
| 391 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/gcp\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/gcpsecretmanager\"\n)", |
| 392 |
"code": "// Your GCP credentials.\n// See https://cloud.google.com/docs/authentication/production\n// for more info on alternatives.\ncreds, err := gcp.DefaultCredentials(ctx)\nif err != nil {\n\treturn err\n}\n\n// Connect to the GCP Secret Manager service.\nclient, cleanup, err := gcpsecretmanager.Dial(ctx, creds.TokenSource)\nif err != nil {\n\treturn err\n}\ndefer cleanup()\n\n// You can use the SecretKey helper to construct a secret key from\n// your project ID and the secret ID; alternatively,\n// you can construct the full string yourself (e.g.,\n// \"projects/gcp-project-id/secrets/secret-id\").\n// gcpsecretmanager package will always use the latest secret value,\n// so `/version/latest` postfix must NOT be added to the secret key.\n// See https://cloud.google.com/secret-manager\n// for more details.\n//\n// For this example, the GCP Secret Manager secret being\n// referenced should have a JSON string that decodes into MyConfig.\nvariableKey := gcpsecretmanager.SecretKey(\"gcp-project-id\", \"secret-id\")\n\n// Construct a *runtimevar.Variable that watches the variable.\nv, err := gcpsecretmanager.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 393 |
}, |
| 394 |
"gocloud.dev/runtimevar/gcpsecretmanager.Example_openVariableFromURL": { |
| 395 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/gcpsecretmanager\"\n)", |
| 396 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The URL Host+Path are used as the GCP Secret Manager secret key;\n// see https://cloud.google.com/secret-manager\n// for more details.\n\nv, err := runtimevar.OpenVariable(ctx, \"gcpsecretmanager://projects/myproject/secrets/mysecret?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 397 |
}, |
| 398 |
"gocloud.dev/runtimevar/hashivault.ExampleOpenVariable": { |
| 399 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/hashicorp/vault/api\"\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/hashivault\"\n)", |
| 400 |
"code": "// Get a client to use with the Vault API.\nclient, err := hashivault.Dial(ctx, \u0026hashivault.Config{\n\tToken: \"CLIENT_TOKEN\",\n\tAPIConfig: api.Config{\n\t\tAddress: \"http://127.0.0.1:8200\",\n\t},\n})\nif err != nil {\n\treturn err\n}\n\n// Construct a *runtimevar.Variable that watches the secret.\nv, err := hashivault.OpenVariable(client, \"myapp/config\", runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 401 |
}, |
| 402 |
"gocloud.dev/runtimevar/hashivault.Example_openVariableFromURL": { |
| 403 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/hashivault\"\n)", |
| 404 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The default opener connects to a Vault server based on the environment\n// variables VAULT_SERVER_URL/VAULT_ADDR and VAULT_SERVER_TOKEN/VAULT_TOKEN.\nv, err := runtimevar.OpenVariable(ctx, \"hashivault://myapp/config?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 405 |
}, |
| 406 |
"gocloud.dev/runtimevar/httpvar.ExampleOpenVariable": { |
| 407 |
"imports": "import (\n\t\"net/http\"\n\n\t\"gocloud.dev/runtimevar\"\n\t\"gocloud.dev/runtimevar/httpvar\"\n)", |
| 408 |
"code": "// Create an HTTP.Client\nhttpClient := http.DefaultClient\n\n// Construct a *runtimevar.Variable that watches the page.\nv, err := httpvar.OpenVariable(httpClient, \"http://example.com\", runtimevar.StringDecoder, nil)\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 409 |
}, |
| 410 |
"gocloud.dev/runtimevar/httpvar.Example_openVariableFromURL": { |
| 411 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/runtimevar\"\n\t_ \"gocloud.dev/runtimevar/httpvar\"\n)", |
| 412 |
"code": "// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.\n// The default opener connects to an etcd server based on the environment\n// variable ETCD_SERVER_URL.\n\nv, err := runtimevar.OpenVariable(ctx, \"http://myserver.com/foo.txt?decoder=string\")\nif err != nil {\n\treturn err\n}\ndefer v.Close()" |
| 413 |
}, |
| 414 |
"gocloud.dev/secrets.ExampleKeeper_Decrypt": { |
| 415 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n)", |
| 416 |
"code": "var cipherText []byte // obtained from elsewhere and random-looking\nplainText, err := keeper.Decrypt(ctx, cipherText)\nif err != nil {\n\treturn err\n}" |
| 417 |
}, |
| 418 |
"gocloud.dev/secrets.ExampleKeeper_Encrypt": { |
| 419 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n)", |
| 420 |
"code": "plainText := []byte(\"Secrets secrets...\")\ncipherText, err := keeper.Encrypt(ctx, plainText)\nif err != nil {\n\treturn err\n}" |
| 421 |
}, |
| 422 |
"gocloud.dev/secrets/awskms.ExampleOpenKeeper": { |
| 423 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/aws/aws-sdk-go-v2/config\"\n\t\"gocloud.dev/secrets/awskms\"\n)", |
| 424 |
"code": "// Establish a AWS V2 Config.\n// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.\nctx := context.Background()\ncfg, err := config.LoadDefaultConfig(ctx)\nif err != nil {\n\treturn err\n}\n\n// Get a client to use with the KMS API.\nclient, err := awskms.Dial(cfg)\nif err != nil {\n\treturn err\n}\n\n// Construct a *secrets.Keeper.\nkeeper := awskms.OpenKeeper(client, \"alias/test-secrets\", nil)\ndefer keeper.Close()" |
| 425 |
}, |
| 426 |
"gocloud.dev/secrets/awskms.Example_openFromURL": { |
| 427 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n\t_ \"gocloud.dev/secrets/awskms\"\n)", |
| 428 |
"code": "// Use one of the following:\n\n// 1. By ID.\nkeeperByID, err := secrets.OpenKeeper(ctx,\n\t\"awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1\")\nif err != nil {\n\treturn err\n}\ndefer keeperByID.Close()\n\n// 2. By alias.\nkeeperByAlias, err := secrets.OpenKeeper(ctx,\n\t\"awskms://alias/ExampleAlias?region=us-east-1\")\nif err != nil {\n\treturn err\n}\ndefer keeperByAlias.Close()\n\n// 3. By ARN. Note that ARN may contain \":\" characters, which cannot be escaped\n// in the Host part of a URL, so the \"awskms:///\u003cARN\u003e\" form should be used.\nconst arn = \"arn:aws:kms:us-east-1:111122223333:key/\" +\n\t\"1234abcd-12ab-34bc-56ef-1234567890ab\"\nkeeperByARN, err := secrets.OpenKeeper(ctx,\n\t\"awskms:///\"+arn+\"?region=us-east-1\")\nif err != nil {\n\treturn err\n}\ndefer keeperByARN.Close()" |
| 429 |
}, |
| 430 |
"gocloud.dev/secrets/azurekeyvault.ExampleOpenKeeper": { |
| 431 |
"imports": "import \"gocloud.dev/secrets/azurekeyvault\"", |
| 432 |
"code": "// Makes a client to use with the Azure KeyVault API, using default\n// authorization from the environment.\nclientMaker := azurekeyvault.DefaultClientMaker\n\n// Construct a *secrets.Keeper.\nkeeper, err := azurekeyvault.OpenKeeper(clientMaker, \"https://mykeyvaultname.vault.azure.net/keys/mykeyname\", nil)\nif err != nil {\n\treturn err\n}\ndefer keeper.Close()" |
| 433 |
}, |
| 434 |
"gocloud.dev/secrets/azurekeyvault.Example_openFromURL": { |
| 435 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n\t_ \"gocloud.dev/secrets/azurekeyvault\"\n)", |
| 436 |
"code": "// The \"azurekeyvault\" URL scheme is replaced with \"https\" to construct an Azure\n// Key Vault keyID, as described in https://docs.microsoft.com/en-us/azure/key-vault/about-keys-secrets-and-certificates.\n// You can add an optional \"/{key-version}\" to the path to use a specific\n// version of the key; it defaults to the latest version.\nkeeper, err := secrets.OpenKeeper(ctx, \"azurekeyvault://mykeyvaultname.vault.azure.net/keys/mykeyname\")\nif err != nil {\n\treturn err\n}\ndefer keeper.Close()" |
| 437 |
}, |
| 438 |
"gocloud.dev/secrets/gcpkms.ExampleOpenKeeper": { |
| 439 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets/gcpkms\"\n)", |
| 440 |
"code": "// Get a client to use with the KMS API.\nclient, done, err := gcpkms.Dial(ctx, nil)\nif err != nil {\n\treturn err\n}\n// Close the connection when done.\ndefer done()\n\n// You can also use gcpkms.KeyResourceID to construct this string.\nconst keyID = \"projects/MYPROJECT/\" +\n\t\"locations/MYLOCATION/\" +\n\t\"keyRings/MYKEYRING/\" +\n\t\"cryptoKeys/MYKEY\"\n\n// Construct a *secrets.Keeper.\nkeeper := gcpkms.OpenKeeper(client, keyID, nil)\ndefer keeper.Close()" |
| 441 |
}, |
| 442 |
"gocloud.dev/secrets/gcpkms.Example_openFromURL": { |
| 443 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n\t_ \"gocloud.dev/secrets/gcpkms\"\n)", |
| 444 |
"code": "keeper, err := secrets.OpenKeeper(ctx,\n\t\"gcpkms://projects/MYPROJECT/\"+\n\t\t\"locations/MYLOCATION/\"+\n\t\t\"keyRings/MYKEYRING/\"+\n\t\t\"cryptoKeys/MYKEY\")\nif err != nil {\n\treturn err\n}\ndefer keeper.Close()" |
| 445 |
}, |
| 446 |
"gocloud.dev/secrets/hashivault.ExampleOpenKeeper": { |
| 447 |
"imports": "import (\n\t\"context\"\n\n\t\"github.com/hashicorp/vault/api\"\n\t\"gocloud.dev/secrets/hashivault\"\n)", |
| 448 |
"code": "// Get a client to use with the Vault API.\nclient, err := hashivault.Dial(ctx, \u0026hashivault.Config{\n\tToken: \"CLIENT_TOKEN\",\n\tAPIConfig: api.Config{\n\t\tAddress: \"http://127.0.0.1:8200\",\n\t},\n})\nif err != nil {\n\treturn err\n}\n\n// Construct a *secrets.Keeper.\nkeeper := hashivault.OpenKeeper(client, \"my-key\", nil)\ndefer keeper.Close()" |
| 449 |
}, |
| 450 |
"gocloud.dev/secrets/hashivault.Example_openFromURL": { |
| 451 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n\t_ \"gocloud.dev/secrets/hashivault\"\n)", |
| 452 |
"code": "keeper, err := secrets.OpenKeeper(ctx, \"hashivault://mykey\")\nif err != nil {\n\treturn err\n}\ndefer keeper.Close()" |
| 453 |
}, |
| 454 |
"gocloud.dev/secrets/localsecrets.ExampleNewKeeper": { |
| 455 |
"imports": "import \"gocloud.dev/secrets/localsecrets\"", |
| 456 |
"code": "secretKey, err := localsecrets.NewRandomKey()\nif err != nil {\n\treturn err\n}\nkeeper := localsecrets.NewKeeper(secretKey)\ndefer keeper.Close()" |
| 457 |
}, |
| 458 |
"gocloud.dev/secrets/localsecrets.Example_openFromURL": { |
| 459 |
"imports": "import (\n\t\"context\"\n\n\t\"gocloud.dev/secrets\"\n\t_ \"gocloud.dev/secrets/localsecrets\"\n)", |
| 460 |
"code": "// Using \"base64key://\", a new random key will be generated.\nrandomKeyKeeper, err := secrets.OpenKeeper(ctx, \"base64key://\")\nif err != nil {\n\treturn err\n}\ndefer randomKeyKeeper.Close()\n\n// Otherwise, the URL hostname must be a base64-encoded key, of length 32 bytes when decoded.\n// Note that base64.URLEncode should be used, to avoid URL-unsafe characters.\nsavedKeyKeeper, err := secrets.OpenKeeper(ctx, \"base64key://smGbjm71Nxd1Ig5FS0wj9SlbzAIrnolCz9bQQ6uAhl4=\")\nif err != nil {\n\treturn err\n}\ndefer savedKeyKeeper.Close()" |
| 461 |
}, |
| 462 |
"gocloud.dev/server.ExampleServer": { |
| 463 |
"imports": "import (\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"gocloud.dev/server\"\n)", |
| 464 |
"code": "// Use the constructor function to create the server.\nsrv := server.New(http.DefaultServeMux, nil)\n\n// Register a route.\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Hello, World!\")\n})\n\n// Start the server. If ListenAndServe returns an error, print it and exit.\nif err := srv.ListenAndServe(\":8080\"); err != nil {\n\tlog.Fatalf(\"%v\", err)\n}" |
| 465 |
}, |
| 466 |
"gocloud.dev/server.ExampleServer_withHealthChecks": { |
| 467 |
"imports": "import (\n\t\"fmt\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"gocloud.dev/server\"\n\t\"gocloud.dev/server/health\"\n)", |
| 468 |
"code": "// Create a health.Checker from the type we defined for our application.\n// In this example, healthCheck will report the server is unhealthy for 10 seconds\n// after startup, and as healthy henceforth. Check the /healthz/readiness\n// HTTP path to see readiness.\nhealthCheck := new(customHealthCheck)\ntime.AfterFunc(10*time.Second, func() {\n\thealthCheck.mu.Lock()\n\tdefer healthCheck.mu.Unlock()\n\thealthCheck.healthy = true\n})\n\n// The server.Options struct takes a slice of health checks, because you\n// may need to check several things.\nsrvOptions := \u0026server.Options{\n\tHealthChecks: []health.Checker{healthCheck},\n}\n\n// Pass the options to the Server constructor.\nsrv := server.New(http.DefaultServeMux, srvOptions)\n\n// Register a route.\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Hello, World!\")\n})\n\n// Start the server. You will see requests logged to STDOUT.\nif err := srv.ListenAndServe(\":8080\"); err != nil {\n\tlog.Fatalf(\"%v\", err)\n}" |
| 469 |
}, |
| 470 |
"gocloud.dev/server.ExampleServer_withRequestLogger": { |
| 471 |
"imports": "import (\n\t\"fmt\"\n\t\"net/http\"\n\t\"os\"\n\n\t\"gocloud.dev/server\"\n\t\"gocloud.dev/server/requestlog\"\n)", |
| 472 |
"code": "// Create a logger, and assign it to the RequestLogger field of a\n// server.Options struct.\nsrvOptions := \u0026server.Options{\n\tRequestLogger: requestlog.NewNCSALogger(os.Stdout, func(error) {}),\n}\n\n// Pass the options to the Server constructor.\nsrv := server.New(http.DefaultServeMux, srvOptions)\n\n// Register a route.\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Hello, World!\")\n})\n\n// Start the server. You will see requests logged to STDOUT.\nif err := srv.ListenAndServe(\":8080\"); err != nil {\n\tlog.Fatalf(\"%v\", err)\n}" |
| 473 |
} |
| 474 |
} |