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

Turkish 02.3.md almost done

上级 357d0aaa
......@@ -224,11 +224,11 @@ Yukarıdaki örnekten tahmin edebileceğiniz üzere aşağıda açıklamaları b
Yukarıdaki örnek fonksiyon `max` da iki aynı tür parametre `int` olduğu için bir tane yazmak yeterli olur. Yani `a int, b int` yerine `a, b int` kullanılır. Birden fazla parametre için de aynı kural geçerlidir. Farkettiyseniz `max` fonksiyonu sadece bir değer döndürür ve zorunda olmadığımız için o değere bir isim vermedik, bu kısa halini kullandık.
### Multi-value return
### Çok değerli döndürme
One thing that Go is better at than C is that it supports multi-value returns.
Go'nun C'den iyi olduğu bir şey birden fazla değer döndürmeyi desteklemesidir.
We'll use the following example here.
Alttaki örnekte bunu kullanalım.
package main
import "fmt"
......@@ -248,7 +248,7 @@ We'll use the following example here.
fmt.Printf("%d * %d = %d\n", x, y, xTIMESy)
}
The above example returns two values without names -you have the option of naming them also. If we named the return values, we would just need to use `return` to return the values since they are initialized in the function automatically. Notice that if your functions are going to be used outside of the package, which means your function names start with a capital letter, you'd better write complete statements for `return`; it makes your code more readable.
Üstteki fonksiyon isimsiz iki değer döndürür -isterseniz isim verebilirsiniz. Eğer isimlendirseydik, `return` yazıp isimlerini yazmamız yeterdi. Çünkü fonksiyonun içinde tanımlılar. Şuna dikkat etmelisiniz ki eğer fonksiyonu başka bir pakette kullanacaksanız (fonksiyonun ilk harfi büyük harfle başlamalıdır) `return` yapacaklarınızı tam bir ifade olarak yazmanız daha iyi olacaktır. Kodu daha okunur hale getirir.
func SumAndProduct(A, B int) (add int, multiplied int) {
add = A+B
......@@ -256,23 +256,23 @@ The above example returns two values without names -you have the option of namin
return
}
### Variadic functions
### Variadic fonksiyonlar
Go supports functions with a variable number of arguments. These functions are called "variadic", which means the function allows an uncertain numbers of arguments.
Go birden fazla argüman alabilen fonksiyonları destekler. Bunlara variadic (belirsiz-değişen sayıda argüman alan) fonksiyon denir.
func myfunc(arg ...int) {}
`arg …int` tells Go that this is a function that has variable arguments. Notice that these arguments are type `int`. In the body of function, the `arg` becomes a `slice` of `int`.
`arg …int` kısmı Go ya bu fonksiyonun değişen sayıda argüman aldığını söyler. Bu argümanların türü `int` dir. `arg` fonksiyonun gövdesinde `int` türünde bir `slice` olur.
for _, n := range arg {
fmt.Printf("And the number is: %d\n", n)
}
### Pass by value and pointers
### Değer ile devretmek ve pointerlar
When we pass an argument to the function that was called, that function actually gets the copy of our variables so any change will not affect to the original variable.
Bir fonksiyon çağırıp ona argüman verdiğimizde o fonksiyon aslında değişkenin bir kopyasını alır. Dolayısı ile yapılan işlemler değişkende bir değişiklik yaratmaz.
Let's see one example in order to prove what i'm saying.
Bunun kanıtı olarak bir örnek görelim.
package main
import "fmt"
......@@ -293,14 +293,14 @@ Let's see one example in order to prove what i'm saying.
fmt.Println("x+1 = ", x1) // should print "x+1 = 4"
fmt.Println("x = ", x) // should print "x = 3"
}
Can you see that? Even though we called `add1` with `x`, the origin value of `x` doesn't change.
The reason is very simple: when we called `add1`, we gave a copy of `x` to it, not the `x` itself.
Gördünüz mü? `add1` fonksiyonuna `x` i gönderdiğimiz halde asıl değeri değişmedi.
Sebebi basit: `add1` i çağırdığımızda ona `x` in bir kopyasını gönderdik `x` in kendisini değil.
Now you may ask how I can pass the real `x` to the function.
Şimdi sorabilirsiniz `x` in kendisini nasıl fonksiyona verebilirim diye.
We need use pointers here. We know variables are stored in memory and they have some memory addresses. So, if we want to change the value of a variable, we must change its memory address. Therefore the function `add1` has to know the memory address of `x` in order to change its value. Here we pass `&x` to the function, and change the argument's type to the pointer type `*int`. Be aware that we pass a copy of the pointer, not copy of value.
Burada pointer kullanmamız gerekiyor. Biliyoruz ki değişkenler bellekte tutulur ve bir adresleri vardır. Eğer değişkenin aslını değiştirmek istiyorsak onun bellek adresini kullanmalıyız. Böylelikle `add1` fonksiyonu `x` in adresini kullanarak onun değerini değiştirebilir. Parametre türünü `*int` olarak değiştiriyoruz ve değişkenin adresini `&x` ile fonksiyona veriyoruz. Fonksiyona değerin bir kopyasını değil de bir pointer verdiğimize dikkat edin.
package main
import "fmt"
......@@ -321,16 +321,16 @@ We need use pointers here. We know variables are stored in memory and they have
fmt.Println("x+1 = ", x1) // should print "x+1 = 4"
fmt.Println("x = ", x) // should print "x = 4"
}
Now we can change the value of `x` in the functions. Why do we use pointers? What are the advantages?
- 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` 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)
Şimdi `x` in asıl değerini değiştirebiliriz. Neden pointer kullanıyoruz? Avantajı nedir?
- Bir değişken üzerinde çalışmak için birden fazla fonksiyon kullanabilmemize olanak sağlar.
- Adres (8 byte) verdiğimiz için maliyet azalır. Değerin kopyasını vermek maliyet ve zaman için verimli bir yöntem değildir.
- `string`, `slice`, `map` referans türlerdir. Yani fonksiyona verilirken standart olarak pointer olarak verilirler. (Dikkat: Eğer `slice` ın uzunluğunu değiştirmek istiyorsanız açıkça pointer olarak vermeniz gerekir.)
### defer
Go has a well designed keyword called `defer`. You can have many `defer` statements in one function; they will execute in reverse order when the program executes to the end of functions. In the case where the program opens some resource files, these files would have to be closed before the function can return with errors. Let's see some examples.
Go da iyi tasarlanmış `defer` (ertelemek) adlı bir terim vardır. Bir fonksiyonda birden fazla `defer` ifadesi bulunabilir, program çalıştığında sondan başa sırayla çalışacaklardır. Programın dosya açtığı durumlarda bu dosyaların hata vermeden önce kapatılması gerekir. Örneklere bakalım.
func ReadWrite() bool {
file.Open("file")
......@@ -348,8 +348,8 @@ Go has a well designed keyword called `defer`. You can have many `defer` stateme
file.Close()
return true
}
We saw some code being repeated several times. `defer` solves this problem very well. It doesn't only help you to write clean code but also makes your code more readable.
Bazı kodların tekrar ettiğini görüyoruz. `defer` bu problemi çok iyi çözer. Daha temiz kod yazmanıza yardım etmekle kalmaz kodunuzu daha okunur yapar.
func ReadWrite() bool {
file.Open("file")
......@@ -362,20 +362,20 @@ We saw some code being repeated several times. `defer` solves this problem very
}
return true
}
If there are more than one `defer`s, they will execute by reverse order. The following example will print `4 3 2 1 0`.
Eğer birden fazla `defer` varsa ters sırayla çalışırlar. Sıradaki örnek `4 3 2 1 0` sonucunu yazar.
for i := 0; i < 5; i++ {
defer fmt.Printf("%d ", i)
}
### Functions as values and types
### Değer ve tür olarak fonksiyonlar
Functions are also variables in Go, we can use `type` to define them. Functions that have the same signature can be seen as the same type.
Go'da fonksiyonlar aynı zamanda değişken olabilirler. `type` onları kullanarak tanımlayabiliriz. Aynı imzaya sahip fonksiyonlar ayno tür olarak görülebilir.
type typeName func(input1 inputType1 , input2 inputType2 [, ...]) (result1 resultType1 [, ...])
What's the advantage of this feature? The answer is that it allows us to pass functions as values.
Bunun avantajı nedir? Cevap, fonksiyonları değer olarak verebilmemizi sağlamasıdır.
package main
import "fmt"
......@@ -416,6 +416,8 @@ What's the advantage of this feature? The answer is that it allows us to pass fu
even := filter(slice, isEven)
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.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册