From 50c7611245e9a1a721c62973da6a2d5e737eb61b Mon Sep 17 00:00:00 2001 From: mainul35 Date: Fri, 27 Aug 2021 22:10:09 +0700 Subject: [PATCH] Array practice --- Array.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Array.go b/Array.go index 1938747..8daf42b 100644 --- a/Array.go +++ b/Array.go @@ -3,12 +3,28 @@ package main import "fmt" func main() { - var nums [10]int + // Approach 1: inserting with index number + // Only applicable for fixed sized array + var fixedNums [10]int + for i := 0; i < 10; i++ { + fixedNums[i] = i + 1 + } + // For fixed sized array, + // we must pass like below + fmt.Println("============= Fixed size array =============") + PrintValue(fixedNums[:]) + + // Approach 2: With append(...) + var nums []int for i := 0; i < 10; i++ { - nums[i] = i + 1 + nums = append(nums, i+1) } - PrintValue(nums[:]) + + // For dynamic sized array, + // we can also pass the array like below + fmt.Println("============= Dynamic size array =============") + PrintValue(nums) } func PrintValue(arr []int) {