يوفر نظام Go طريقة سهلة للغاية لتوصيف تطبيقاتك.
سأشرح التنميط باستخدام حزمة بواسطة Dave Cheney مما يجعل من السهل جدًا تصحيح البرامج ، عن طريق إضافة سطر واحد إلى موقعنا.main()
.
كل ما تحتاجه للبدء هو اتباع هذه الخطوات السهلة.
تشكيل وحدة المعالجة المركزية
الخطوة رقم 1: التنزيلgithub.com/pkg/profile
لا يمكن أن يكون أبسط من الجري
go get github.com/pkg/profile
وانت انتهيت.
الخطوة رقم 2: أضف التنميط إلى ملفmain()
وظيفة الأمر الخاص بك
package main
import (
//…
“github.com/pkg/profile”
)
func main() {
// CPU profiling by default
defer profile.Start().Stop()
<span style="color:#75715e">//...
}
الخطوة # 3: بناء وتشغيل البرنامج الخاص بك
سيؤدي هذا إلى إنشاء ملف*.pprof
ملف في مجلد temp ، ويخبرك بمكانه (ستكون هناك حاجة لاحقًا)
2017/08/03 14:26:28 profile: cpu profiling enabled, /var/...../cpu.pprofStep #4: install graphviz
if you don’t have it installed yet
This is used to generate the graph on a pdf.
On a Mac, it’s a simple brew install graphviz
. Refer to https://www.graphviz.org for other platforms.
Step #5: run go tool pprof
Pass your binary location, and the location of the cpu.pprof
file as returned when running your program.
You can generate the analysis in various formats. The PDF one is pretty amazing:
go tool pprof --pdf ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.pdf

You can generate other kind of visualizations as well, e.g. txt
:
go tool pprof --txt ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.txt
Memory profiling
Memory profiling is essentially the same as CPU profiling, but instead of using the default configuration for profile.Start()
, we pass a profile.MemProfile
flag:
defer profile.Start(profile.MemProfile).Stop()
thus the code becomes
package main
import (
//…
“github.com/pkg/profile”
)
func main() {
// Memory profiling
defer profile.Start(profile.MemProfile).Stop()
<span style="color:#75715e">//...
}
and when running the program, it will generate a mem.pprof
file instead of cpu.pprof
.

Read more about profiling Go apps
This is just a start. Read more at:
- https://blog.golang.org/profiling-go-programs
- Lower level: https://golang.org/pkg/runtime/pprof/
- More options for advanced usage of
github.com/pkg/profile
http://godoc.org/github.com/pkg/profile
More go tutorials:
- Using NGINX Reverse Proxy to serve Go services
- Making a copy of a struct in Go
- The basics of a Go Web Server
- Sorting a map type in Go
- Go pointers in a nutshell
- Go Tags explained
- Go Date and Time Formatting
- JSON processing with Go
- Go Variadic Functions
- Go Strings Cheat Sheet
- The Go Empty Interface Explained
- Debugging Go with VS Code and Delve
- Named Go returns parameters
- Generating random numbers and strings in Go
- Filesystem Structure of a Go project
- Binary Search Algorithm Implemented in Go
- Using Command Line Flags in Go
- GOPATH Explained
- Build a Command Line app with Go: lolcat
- Building a CLI command with Go: cowsay
- Using Shell Pipes with Go
- Go CLI tutorial: fortune clone
- List the files in a folder with Go
- Use Go to get a list of repositories from GitHub
- Go, append a slice of strings to a file
- Go, convert a string to a bytes slice
- Visualize your local Git contributions with Go
- Getting started with Go CPU and memory profiling
- Solving the "does not support indexing" error in a Go program
- Measuring execution time in a Go program
- Building a Web Crawler with Go to detect duplicate titles
- Go Best Practices: Pointer or value receivers?
- Go Best Practices: Should you use a method or a function?
- Go Data Structures: Set
- Go Maps Cheat Sheet
- Generate implementations for generic types in Go
- Go Data Structures: Dictionary
- Go Data Structures: Hash Table
- Implement Events Listeners in Go through Channels
- Go Data Structures: Stack
- Go Data Structures: Queue
- Go Data Structures: Binary Search Tree
- Go Data Structures: Graph
- Go Data Structures: Linked List
- The complete guide to Go Data Structures
- Comparing Go Values
- Is Go object oriented?
- Working with a SQL Database in Go
- Using environment variables in Go
- Go tutorial: REST API backed by PostgreSQL
- Enabling CORS on a Go Web Server
- Deploying a Go Application in a Docker Container
- Why Go is a powerful language to learn as a PHP developer
- Go, remove the io.Reader.ReadString newline char
- Go, how to watch changes and rebuild your program
- Go, count the months since a date
- Accessing HTTP POST parameters in Go