DEV Community

Aydın Yakar
Aydın Yakar

Posted on • Edited on

5 1

Golang 101: Diziler (Arrays)

Diziler aynı türden verileri tutmak için uygundur. İndis (index) değerleri 0'dan başlarlar.

package main

import "fmt"

func main() {

    // Dizi tanımlama
    var i [4]int
    fmt.Println("Boş dizi:", i)

    // Değer atama
    i[3] = 100
    fmt.Println("Atama sonrası:", i)

    // Dizi boyutu
    fmt.Println("Dizi boyutu:", len(i))

    // Diziye tanımlarken değer atama
    x := [5]int{1, 2, 3, 4, 5}
    fmt.Println("Dizi:", x)

    // Çok boyutlu dizi
    var y [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            y[i][j] = i + j
        }
    }
    fmt.Println("2 boyutlu dizi: ", y)
}
Enter fullscreen mode Exit fullscreen mode
Boş dizi: [0 0 0 0]
Atama sonrası: [0 0 0 100]
Dizi boyutu: 4
Dizi: [1 2 3 4 5]
2 boyutlu dizi:  [[0 1 2] [1 2 3]]
Enter fullscreen mode Exit fullscreen mode

çalıştır!

Dizi Tanımlama

    var i [4]int
Enter fullscreen mode Exit fullscreen mode

4 değer alabilen integer bir dizi tanımı yukarıdaki gibi yapılır.

Diziye Değer Atama

    i[3] = 100
Enter fullscreen mode Exit fullscreen mode

Dizinin 3. elemanına (indis'ine) değer atanması

Dizi Boyutu

    fmt.Println("Dizi boyutu:", len(i))
Enter fullscreen mode Exit fullscreen mode

Dizideki eleman sayısı len ile bulunabilir.

Diziyi Tanımlarken Değer Atama

    x := [5]int{1, 2, 3, 4, 5}
Enter fullscreen mode Exit fullscreen mode

Değişken tanımlarında olduğu gibi dizi tanımlarında da tanımlama ve değer atama işlemleri birlikte yapılabilir.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay