提交 76e7b5d2 编写于 作者: B Bora Kaplan 提交者: GitHub

Translated 02.3.md to Turkish

Code blocks are not translated yet.
上级 f9ca488e
......@@ -417,19 +417,17 @@ Bunun avantajı nedir? Cevap, fonksiyonları değer olarak verebilmemizi sağlam
fmt.Println("Even elements of slice are: ", even)
}
Interface kullanılan durumlarda çok yararlıdır. Gördüğünüz gibi `testInt`
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.
Interface kullanılan durumlarda çok yararlıdır. Gördüğünüz gibi `testInt` fonksiyon türünde bir değişkendir ve `filter` ın döndürdüğü argümanlar ve değerler `testInt` ile aynıdır. Böylelikle programlarımızda karmaşık mantık yürütebilir ve esneklik kazanabiliriz.
### Panic and Recover
### Panic ve 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.
Go da Java gibi `try-catch` yapısı yoktur. Go exception fırlatmak yerine `panic` ve `recover` ile hatalarla ilgilenir. Etkili olmasına karşın `panic` çok fazla kullanılmamalıdır.
`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` programın normal akışını durdurmak ve panik haline sokmak için kullanılır. `F` adında bir fonksiyon `panic` çağırdığında fonksiyon çalışmayı durdurur ve ertelenen `defer` fonksiyonları çalıştırılır. Sonra `F` panik halinin başladığı yere döner. Bütün fonksiyonlar `F` gibi başladığı yerdeki `goroutine` e dönmeden program sonlanmaz. `panic` programın içinde `panic` olarak çağırıldığında gerçekleşir. Ayrıca dizi sınırları dışına erişim hataları gibi bazı hatalar da `panic` sebebi olabilir.
`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` (kurtarmak) `goroutine` leri panik halinden kurtarmak için kullanılır. `defer` fonksiyonlarının içinde `recover` çağırmak yararlıdır çünkü panik halinde normal fonksiyonlar çalışmayı durdurur. Çağırıldığında varsa `panic` değerlerini yakalar yoksa `nil` yakalar.
The following example shows how to use `panic`.
Aşağıdaki örnekte `panic` nasıl kullanılır gösterilmiştir.
var user = os.Getenv("USER")
......@@ -438,8 +436,8 @@ The following example shows how to use `panic`.
panic("no value for $USER")
}
}
The following example shows how to check `panic`.
Bu örnek de `panic` kontrolü yapılmasını gösterir.
func throwsPanic(f func()) (b bool) {
defer func() {
......@@ -451,67 +449,69 @@ The following example shows how to check `panic`.
return
}
### `main` function and `init` function
### `main` ve `init` fonksiyonları
Go has two retentions which are called `main` and `init`, where `init` can be used in all packages and `main` can only be used in the `main` package. These two functions are not able to have arguments or return values. Even though we can write many `init` functions in one package, I strongly recommend writing only one `init` function for each package.
`init` fonksiyonu tüm paketlerde kullanılabilirken `main` fonksiyonu sadece `main` paketinde kullanılabilir. Bu iki fonksiyonun parametreleri yoktur ve değer döndürmezler. Bir paket içinde birden çok `init` yazılabilse de sadece bir tane yazılmasını kesinlikle tavsiye ederim.
Go programs will call `init()` and `main()` automatically, so you don't need to call them by yourself. For every package, the `init` function is optional, but `package main` has one and only one `main` function.
Go programları `init()` ve `main()` i otomatik çağırır. Her paket için `init` fonksiyonu isteğe bağlı iken `main` paketi için sadece bir tane `main` fonksiyonu kesinlikle bulunmak zorundadır.
Programs initialize and begin execution from the `main` package. If the `main` package imports other packages, they will be imported in the compile time. If one package is imported many times, it will be only compiled once. After importing packages, programs will initialize the constants and variables within the imported packages, then execute the `init` function if it exists, and so on. After all the other packages are initialized, programs will initialize constants and variables in the `main` package, then execute the `init` function inside the package if it exists. The following figure shows the process.
Programlar işleme `main` paketinden başlar. Eğer `main` başka paketler dahil (import) ediyorsa derleme sürecinde onlar da dahil edilir. Bir paket birden fazla kez dahil edildiyse bile sadece bir kere derlenir. Paketler dahil edildikten sonra program bu paketlerdeki değişkenleri ve sabitleri parafe eder ve sonra varsa `init` çalıştırılır ve böyle devam eder. Bütün paketler parafe edildikten sonra program `main` paketindeki değişkenleri ve sabitleri parafe eder ve varsa `init` çalıştırılır. Alttaki şekil bu süreci gösterir.
![](images/2.3.init.png?raw=true)
Figure 2.6 Flow of programs initialization in Go
Figure 2.6 Go da programların parafe edilmesinin akışı
### import
We use `import` very often in Go programs as follows.
Go programlarında `import` (dahil etmek) aşağıdaki gibi çok sık kullanılır
import(
"fmt"
)
Then we use functions in that package as follows.
Sonra dahil edilen bu paketteki fonksiyonlar aşağıdaki gibi kullanılır.
fmt.Println("hello world")
`fmt` is from Go standard library, it is located within $GOROOT/pkg. Go supports third-party packages in two ways.
1. Relative path
`fmt` Go standart kütüphanesinde bulunur. Bilgisayarda $GOROOT/pkg da bulunur. Go üçüncü şahış paketlerini iki şekilde destekler.
1. Göreceli yol
import "./model" // load package in the same directory, I don't recommend this way.
2. Absolute path
2. Kesin yol
import "shorturl/model" // load package in path "$GOPATH/pkg/shorturl/model"
There are some special operators when we import packages, and beginners are always confused by these operators.
1. Dot operator.
Sometime we see people use following way to import packages.
Paketler dahil edilirken kullanılan özel operatörler vardır ve bunlar genelde yeni başlayanların kafalarını kurcalar.
1. Nokta operatörü
Bazen aşağıdaki gibi kullanıldığını görürüz.
import(
. "fmt"
)
The dot operator means you can omit the package name when you call functions inside of that package. Now `fmt.Printf("Hello world")` becomes to `Printf("Hello world")`.
2. Alias operation.
It changes the name of the package that we imported when we call functions that belong to that package.
Nokta operatörünün görevi, bir paketten fonksiyon çağırırken paketin adının yazılma gereksinimini kaldırmasıdır. Böylelikle `fmt.Printf("Hello world")` yerine `Printf("Hello world")` yazılır.
2. Takma isim verme
Pakete takma isim vererek fonksiyon çağırırken kullanılan ad değiştirilir.
import(
f "fmt"
)
Now `fmt.Printf("Hello world")` becomes to `f.Printf("Hello world")`.
3. `_` operator.
This is the operator that is difficult to understand without someone explaining it to you.
Böylelikle `fmt.Printf("Hello world")` yerine `f.Printf("Hello world")` yazılır.
3. `_` operatorü
Bunu anlamak zordur özellikle biri size açıklamadıysa.
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
The `_` operator actually means we just want to import that package and execute its `init` function, and we are not sure if want to use the functions belonging to that package.
Bu operatör dahil edilen paketin sadece `init` fonksiyonun çağrılıp çalıştırılması için kullanılır. Paketteki diğer fonksiyonları kullanacağınıza emin değilseniz kullanabilirsiniz.
## Links
- [Directory](preface.md)
- Previous section: [Go foundation](02.2.md)
- Next section: [struct](02.4.md)
- Önceki bölüm: [Go foundation](02.2.md)
- Sonraki bölüm: [struct](02.4.md)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册