サクサク読めて、アプリ限定の機能も多数!
トップへ戻る
デスク環境を整える
gobyexample.com
We often need our programs to perform operations on collections of data, like selecting all items that satisfy a given predicate or mapping all items to a new collection with a custom function. In some languages it’s idiomatic to use generic data structures and algorithms. Go does not support generics; in Go it’s common to provide collection functions if and when they are specifically needed for y
In the previous example we used explicit locking with mutexes to synchronize access to shared state across multiple goroutines. Another option is to use the built-in synchronization features of goroutines and channels to achieve the same result. This channel-based approach aligns with Go’s ideas of sharing memory by communicating and having each piece of data owned by exactly 1 goroutine. In this
Reading and writing files are basic tasks needed for many Go programs. First we’ll look at some examples of reading files.
In this example we’ll look at how to implement a worker pool using goroutines and channels. Here’s the worker, of which we’ll run several concurrent instances. These workers will receive work on the jobs channel and send the corresponding results on results. We’ll sleep a second per job to simulate an expensive task. func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs {
Go offers built-in support for regular expressions. Here are some examples of common regexp-related tasks in Go.
Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.
Here we use range to sum the numbers in a slice. Arrays work like this too. nums := []int{2, 3, 4} sum := 0 for _, num := range nums { sum += num } fmt.Println("sum:", sum) range on arrays and slices provides both the index and value for each entry. Above we didn’t need the index, so we ignored it with the blank identifier _. Sometimes we actually want the indexes though.
Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects.
We’ll parse this example URL, which includes a scheme, authentication info, host, port, path, query params, and query fragment.
In the previous example we looked at spawning external processes. We do this when we need an external process accessible to a running Go process. Sometimes we just want to completely replace the current Go process with another (perhaps non-Go) one. To do this we’ll use Go’s implementation of the classic exec function.
Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types.
Go is an open source programming language designed for building scalable, secure and reliable software. Please read the official documentation to learn more. Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below. Unless stated otherwise, examples here assume the latest major release Go. Hello World Values Variable
このページを最初にブックマークしてみませんか?
『Go by Example』の新着エントリーを見る
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く