Go quickstart
Stay organized with collections
Save and categorize content based on your preferences.
Sample profiler_quickstart simulates a CPU-intensive workload for Cloud Profiler in Go.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Go quickstart\n\nSample profiler_quickstart simulates a CPU-intensive workload for Cloud Profiler in Go.\n\nCode sample\n-----------\n\n### Go\n\n\nFor more information, see the\n[Profiler Go API\nreference documentation](https://pkg.go.dev/cloud.google.com/go/profiler).\n\n\nTo authenticate to Profiler, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Sample profiler_quickstart simulates a CPU-intensive workload for profiler.\n package main\n\n import (\n \t\"log\"\n \t\"runtime\"\n\n \t\"cloud.google.com/go/profiler\"\n )\n\n func busyloop() {\n \tfor {\n \t\tload()\n \t\t// Make sure to yield so that the profiler thread\n \t\t// gets some CPU time even on single core machines\n \t\t// where GOMAXPROCS is 1. Not needed in real programs\n \t\t// as typically the preemption happens naturally.\n \t\truntime.Gosched()\n \t}\n }\n\n func load() {\n \tfor i := 0; i \u003c (1 \u003c\u003c 20); i++ {\n \t}\n }\n\n func main() {\n \terr := profiler.https://cloud.google.com/go/docs/reference/cloud.google.com/go/profiler/latest/index.html#cloud_google_com_go_profiler_Start(profiler.https://cloud.google.com/go/docs/reference/cloud.google.com/go/profiler/latest/index.html#cloud_google_com_go_profiler_Config{\n \t\tService: \"hello-profiler\",\n \t\tNoHeapProfiling: true,\n \t\tNoAllocProfiling: true,\n \t\tNoGoroutineProfiling: true,\n \t\tDebugLogging: true,\n \t\t// ProjectID must be set if not running on GCP.\n \t\t// ProjectID: \"my-project\",\n \t})\n \tif err != nil {\n \t\tlog.Fatalf(\"failed to start the profiler: %v\", err)\n \t}\n \tbusyloop()\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=profiler)."]]