Built-in Functions
-
1append(s S, x ...E) S // core type of S is []E1 2 3s0 := []int{0, 0} s1 := append(s0, 3, 5, 7) // append multiple elements s2 is []int{0, 0, 3, 5, 7} s2 := append(s1, s0...) // append a slice s3 is []int{0, 0, 3, 5, 7, 0, 0} -
1 2copy(dst, src []T) int copy(dst []byte, src string) int1 2 3 4 5 6var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7} var s = make([]int, 6) var b = make([]byte, 5) n1 := copy(s, a[0:]) // n1 == 6, s is []int{0, 1, 2, 3, 4, 5} n2 := copy(s, s[2:]) // n2 == 4, s is []int{2, 3, 4, 5, 4, 5} n3 := copy(b, "Hello, World!") // n3 == 5, b is []byte("Hello")
fmt
|
|
|
|
|
|
|
|
|
|
Println(x)is the same asPrintf("%v\n", x)
| arguments to string | writes to io.Writer | writes to standard output | returns the resulting string |
|---|---|---|---|
| Inserts spaces only between non-string arguments | Fprint | Sprint | |
| according to a format specifier | Fprintf | Printf | Sprintf |
| Inserts spaces between its arguments and end newline | Fprintln | Println | Sprintln |
| scans formatted text to yield values | read from os.Stidin | read from io.Reader | read from argument string |
|---|---|---|---|
| treat newlines as spaces | Scan | Fscan | Sscan |
| according to a format string | Scanf | Fscanf | Sscanf |
| newline is newline | Scanln | Fscanln | Sscanln |
log/slog
|
|
|
|
|
|
|
|
|
|
Debugging
Debugging Go Code with GDB
Finding Concurrency Problems with the Race Checker
- Introducing the Go Race Detector - an introduction to the race detector.
- Data Race Detector - a manual for the data race detector.
In Go, there are several standard libraries that are essential to master for effective development. Some of the key standard libraries include:
- fmt: This library provides functions for formatted I/O, which is crucial for printing and formatting text.
- io: The
iopackage contains interfaces and functions for input and output operations, making it fundamental for file handling and data streams. - net: This package is essential for network programming and includes features for creating network clients and servers, working with URLs, and more.
- http: If you’re building web applications, the
net/httppackage is indispensable for creating HTTP servers and clients. - json: For working with JSON data, the
encoding/jsonpackage allows you to marshal and unmarshal data to and from JSON format. - time: This package is used for dealing with time and dates. It’s vital for scheduling tasks, measuring durations, and working with time zones.
- sync: When dealing with concurrency and parallelism, the
syncpackage offers synchronization primitives like mutexes and WaitGroups. - os: For interacting with the operating system, including file operations and environment variables, the
ospackage is a must-know. - flag: The
flagpackage enables you to parse command-line arguments easily, which is important for building command-line tools. - log: If you need to log messages and errors in your applications, the
logpackage provides a basic logging framework. - errors: This package offers functions for creating and handling errors in a more structured manner.
- context: The
contextpackage is used for managing cancellation and deadlines in Go programs, especially in the context of concurrency.
These libraries cover a wide range of functionality and are foundational to most Go projects. Depending on your specific use case, you may also want to explore additional standard libraries and third-party packages to extend Go’s capabilities.
io
io.Writer
|
|
io.MultiWriter
|
|
io.Copy
|
|
io/ioutil
ioutil.ReadFile
|
|
ioutil.WriteFile
|
|
ioutil.ReadAll
|
|
ioutil.ReadDir
|
|
fmt
fmt.Sprint
|
|
fmt.Sprintf
|
|
fmt.Fprintf
|
|
fmt.Println
|
|
fmt.Errorf
|
|
strings
strings.HasSuffix
|
|
strings.Contains
|
|
strings.Split
|
|
strings.Replace
|
|
strings.LastIndex
|
|
strings.ToLower
|
|
strings.NewReader
|
|
strings.TrimSpace
|
|
strings.Index
|
|
strings.HasPrefix
|
|
strings.Index
|
|
strings.TrimPrefix
|
|
strings.TrimSuffix
|
|
strings.SplitAfter
|
|
strings.Builder
|
|
|
|
strings.Join
|
|
bytes
bytes.Buffer
|
|
|
|
bytes.NewReader
|
|
bytes.Split
|
|
bytes.Trim
|
|
bytes.LastIndex
|
|
bytes.Index
|
|
bytes.Replace
|
|
strconv
strconv.Atoi
|
|
strconv.Itoa(width)
|
|
strconv.FormatInt
|
|
|
|
strconv.ParseUint
|
|
strconv.ParseBool
|
|
strconv.FormatBool
|
|
strconv.ParseInt
|
|
strconv.FormatFloat
|
|
regexp
regexp.MustCompile
|
|
|
|
|
|
unicode
unicode.IsSpace
|
|
unicode/utf8
utf8.DecodeRuneInString
|
|
utf8.ValidString
|
|
sort
sort.Strings
|
|
sort.Sort
|
|
time
time.Now
|
|
time.Second
time.Tick
|
|
time.Date
time.Unix
|
|
time.Millisecond
|
|
time.Sleep
time.Nanosecond
|
|
time.Parse
|
|
time.Time
|
|
time.Now().Year()
|
|
math
math.Ceil
|
|
math.MaxInt32
|
|
math.MaxInt8
|
|
math.MaxInt64
|
|
math/rand
rand.Seed
|
|
rand.Intn
|
|
rand.Int63n
|
|
rand.Int
|
|
os
os.Stdout
|
|
os.Signal
|
|
os.Exit
|
|
os.FileInfo
|
|
os.Open
|
|
os.Stat
|
|
os.IsExist
|
|
os.Lstat
|
|
os.Create
os.Chmod
|
|
os.MkdirAll
|
|
os.Args
|
|
os.Getenv
|
|
os.File
|
|
os.ModeDir|os.ModePerm
|
|
os/exec
exec.LookPath
|
|
exec.Command
|
|
os/user
user.Current
|
|
os/signal
signal.Notify
|
|
path
path.Clean
|
|
path/filepath
filepath.Walk
|
|
filepath.ToSlash
|
|
filepath.Join
|
|
filepath.Glob
|
|
filepath.Abs
filepath.Dir
|
|
filepath.Base
|
|
filepath.Ext
|
|
testing
testing.T
|
|
syscall
syscall.SIGINT
syscall.SIGQUIT
syscall.SIGTERM
|
|
log
|
|
|
|
stdlog.New
stdlog.Ldate
stdlog.Ltime
stdlog.Lshortfile
|
|
encoding/json
json.Unmarshal
|
|
|
|
json.Marshal
|
|
crypto/md5
md5.New
|
|
crypto/tls
tls.Config
tls.Client
|
|
net
net.Conn
|
|
net.Dial
|
|
net.SplitHostPort
|
|
net.ParseIP
|
|
net/http
http.Server
|
|
http.NewRequest
http.DefaultClient
|
|
http.Transport
http.Request
|
|
http.Get
|
|
http.StatusOK
http.StatusNotFound
http.StatusBadRequest
http.StatusUnauthorized
http.StatusInternalServerError
|
|
http.StatusSeeOther
|
|
http.StatusTemporaryRedirect
|
|
net/http/httputil
httputil.NewSingleHostReverseProxy
|
|
net/url
url.URL
|
|
url.Parse
|
|
url.PathUnescape
|
|
url.Scheme
url.Host
url.Path
|
|
errors
errors.New
|
|
sync
sync.Mutex
|
|
|
|
runtime
runtime.GOOS
|
|
runtime.Caller
|
|
runtime.FuncForPC
|
|
archive/zip
zip.Writer
|
|
zip.NewWriter
|
|
zip.FileInfoHeader
zip.Deflate
|
|
zip.File
|
|
zip.OpenReader
|
|
flag
|
|
html/template
template.HTML
|
|
|
|
template.Must
template.New
|
|
text/template
template.ParseFiles
|
|