Merge remote-tracking branch 'upstream/master' into ru
This commit is contained in:
@@ -45,7 +45,7 @@ Well, I know this is still not simple enough for you. Let's see how we fix it.
|
||||
*/
|
||||
vname1, vname2, vname3 := v1, v2, v3
|
||||
|
||||
Now it looks much better. Use `:=` to replace `var` and `type`, this is called a brief statement. But wait, it has one limitation: this form can only be used inside of functions. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables and we can use this brief statement in `var()`.
|
||||
Now it looks much better. Use `:=` to replace `var` and `type`, this is called a brief statement. But wait, it has one limitation: this form can only be used inside of functions. You will get compile errors if you try to use it outside of function bodies. Therefore, we usually use `var` to define global variables.
|
||||
|
||||
`_` (blank) is a special variable name. Any value that is given to it will be ignored. For example, we give `35` to `b`, and discard `34`.( ***This example just show you how it works. It looks useless here because we often use this symbol when we get function return values.*** )
|
||||
|
||||
|
||||
@@ -327,7 +327,7 @@ Now we can change the value of `x` in the functions. Why do we use pointers? Wha
|
||||
|
||||
- Allows us to use more functions to operate on one variable.
|
||||
- Low cost by passing memory addresses (8 bytes), copy is not an efficient way, both in terms of time and space, to pass variables.
|
||||
- `string`, `slice`, `map` are reference types, so they use pointers when passing to functions by default. (Attention: If you need to change the length of `slice`, you have to pass pointers explicitly)
|
||||
- `string`, `slice` and `map` are reference types, so they use pointers when passing to functions by default. (Attention: If you need to change the length of `slice`, you have to pass pointers explicitly)
|
||||
|
||||
### defer
|
||||
|
||||
@@ -418,15 +418,15 @@ What's the advantage of this feature? The answer is that it allows us to pass fu
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
}
|
||||
|
||||
It's very useful when we use interfaces. As you can see `testInt` is a variable that has function type, and return values and arguments of `filter` are the same as `testInt`. Therefore, we can have complex logic in our programs, while maintaining flexibility in our code.
|
||||
It's very useful when we use interfaces. As you can see `testInt` is a variable that has a function as type and the returned values and arguments of `filter` are the same as those of `testInt`. Therefore, we can have complex logic in our programs, while maintaining flexibility in our code.
|
||||
|
||||
### Panic and Recover
|
||||
|
||||
Go doesn't have `try-catch` structure like Java does. Instead of throwing exceptions, Go uses `panic` and `recover` to deal with errors. However, you shouldn't use `panic` very much, although it's powerful.
|
||||
|
||||
Panic is a built-in function to break the normal flow of programs and get into panic status. When a function `F` calls `panic`, `F` will not continue executing but its `defer` functions will continue to execute. Then `F` goes back to the break point which caused the panic status. The program will not terminate until all of these functions return with panic to the first level of that `goroutine`. `panic` can be produced by calling `panic` in the program, and some errors also cause `panic` like array access out of bounds errors.
|
||||
`Panic` is a built-in function to break the normal flow of programs and get into panic status. When a function `F` calls `panic`, `F` will not continue executing but its `defer` functions will continue to execute. Then `F` goes back to the break point which caused the panic status. The program will not terminate until all of these functions return with panic to the first level of that `goroutine`. `panic` can be produced by calling `panic` in the program, and some errors also cause `panic` like array access out of bounds errors.
|
||||
|
||||
Recover is a built-in function to recover `goroutine`s from panic status. Calling `recover` in `defer` functions is useful because normal functions will not be executed when the program is in the panic status. It catches `panic` values if the program is in the panic status, and it gets `nil` if the program is not in panic status.
|
||||
`Recover` is a built-in function to recover `goroutine`s from panic status. Calling `recover` in `defer` functions is useful because normal functions will not be executed when the program is in the panic status. It catches `panic` values if the program is in the panic status, and it gets `nil` if the program is not in panic status.
|
||||
|
||||
The following example shows how to use `panic`.
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ At this point, you should know a bit about developping database drivers in Go. O
|
||||
|
||||
## database/sql
|
||||
|
||||
databse/sql defines even more high-level methods on top of database/sql/driver for more convenient database operations, and it suggests that you implement a connection pool.
|
||||
database/sql defines even more high-level methods on top of database/sql/driver for more convenient database operations, and it suggests that you implement a connection pool.
|
||||
|
||||
type DB struct {
|
||||
driver driver.Driver
|
||||
|
||||
@@ -13,7 +13,7 @@ There are a couple of drivers that support MySQL in Go. Some of them implement t
|
||||
I'll use the first driver in the following examples (I use this one in my personal projects too), and I also recommend that you use it for the following reasons:
|
||||
|
||||
- It's a new database driver and supports more features.
|
||||
- Fully supports `databse/sql` interface standards.
|
||||
- Fully supports `database/sql` interface standards.
|
||||
- Supports keepalive, long connections with thread-safety.
|
||||
|
||||
## Samples
|
||||
|
||||
@@ -76,7 +76,7 @@ HTTPプロトコルはステートレスです。同じクライアントの前
|
||||
|
||||
まずRequestパケットの構造を見てみることにしましょう。Requestパケットは3つの部分にわけられます。第一部分はRequest line(リクエスト行)。第二部分はRequest header(リクエストヘッダ)、第三部分はbody(ボディ)と呼ばれます。headerとbodyの間には空行があり、リクエストパケットの例は以下のようなものです。
|
||||
|
||||
GET /domains/example/ HTTP/1.1 //リクエスト業:リクエスト方法 リクエストRUI HTTPプロトコル/プロトコルバージョン
|
||||
GET /domains/example/ HTTP/1.1 //リクエスト行:リクエスト方法 リクエストRUI HTTPプロトコル/プロトコルバージョン
|
||||
Host:www.iana.org //サーバのホスト名
|
||||
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 //ブラウザ情報
|
||||
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 //クライアントが受け取れるmime
|
||||
|
||||
@@ -52,13 +52,13 @@
|
||||
|
||||
上のコードでWebサーバを書くためにはhttpパッケージの2つの関数を呼ぶだけで良いことがわかります。
|
||||
|
||||
>もしあなたが以前PHPプログラマであれば。こう問うかもしれません。我々のnginx、apacheサーバは必要ないのですかと?なぜならこいつは直接tcpポートを関ししますので、nginxがsやることをやってくれます。またsayhelloNameは実は我々が書いたロジック関数ですので、phpの中のコントローラ(controller)関数に近いものです。
|
||||
>もしあなたが以前PHPプログラマであれば。こう問うかもしれません。我々のnginx、apacheサーバは必要ないのですかと?なぜならこいつは直接tcpポートを関ししますので、nginxがやることをやってくれます。またsayhelloNameは実は我々が書いたロジック関数ですので、phpの中のコントローラ(controller)関数に近いものです。
|
||||
|
||||
>もしあなたがPythonプログラマであったのなら、tornadoを聞いたことがあると思います。このコードはそれとよく似ていませんか?ええ、その通りです。GoはPythonのような動的な言語によく似た特性を持っています。Webアプリケーションを書くにはとても便利です。
|
||||
|
||||
>もしあなたがRubyプログラマであったのなら、RORの/script/serverを起動したのと少し似ている事に気づいたかもしれません。
|
||||
|
||||
Goを通じて簡単な数行のコードでwebサーバを立ち上げることができました。さらにこのWebサーバの内部ではマルチスレッドの特性をサポートしています。続く2つの節でGoが以下にWebのマルチスレッドを実現しているのか細かくご紹介します。
|
||||
Goを通じて簡単な数行のコードでwebサーバを立ち上げることができました。さらにこのWebサーバの内部ではマルチスレッドの特性をサポートしています。続く2つの節でGoが如何にWebのマルチスレッドを実現しているのか細かくご紹介します。
|
||||
|
||||
## links
|
||||
* [目次](<preface.md>)
|
||||
|
||||
@@ -114,7 +114,7 @@ package main
|
||||
}
|
||||
}
|
||||
|
||||
上のコードによって、PostgreSQLが`$1`や`$2といった方法によって引数を渡している様子がお分かりいただけるかとおもいます。MySQLの中の`?`ではありません。また、sql.Openではdsn情報のシンタックスがMySQLのドライバでのdsnシンタックスと異なります。そのため、使用される際はこの違いにご注意ください。
|
||||
上のコードによって、PostgreSQLが`$1`や`$2`といった方法によって引数を渡している様子がお分かりいただけるかとおもいます。MySQLの中の`?`ではありません。また、sql.Openではdsn情報のシンタックスがMySQLのドライバでのdsnシンタックスと異なります。そのため、使用される際はこの違いにご注意ください。
|
||||
|
||||
また、pgはLastInsertId関数をサポートしていません。PostgreSQLの内部ではMySQLのインクリメンタルなIDを返すといった実装がないためです。その他のコードはほとんど同じです。
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ XMLは本来ツリー構造のデータ形式なので、対応するgo言語の
|
||||
}
|
||||
|
||||
|
||||
上の例では、xmlファイルを解析して対応するstructオブジェクトにするには`xml.Unmarshal`によって行われました。この過程はどのように実現されているのでしょうか?我々のstruct定義の後の方を見てみると`xml:"serverName"のような内容があることがわかります。これはstructの特徴の一つです。struct tagと呼ばれています。これはリフレクションを補助するために用いられます。`Unmarshal`の定義を見てみましょう:
|
||||
上の例では、xmlファイルを解析して対応するstructオブジェクトにするには`xml.Unmarshal`によって行われました。この過程はどのように実現されているのでしょうか?我々のstruct定義の後の方を見てみると`xml:"serverName"`のような内容があることがわかります。これはstructの特徴の一つです。struct tagと呼ばれています。これはリフレクションを補助するために用いられます。`Unmarshal`の定義を見てみましょう:
|
||||
|
||||
func Unmarshal(data []byte, v interface{}) error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user