Is it possible to initialize slice with specific values?

Yes but you have to use a different syntax.

oneSlice := []int{1, 1, 1, 1, 1}

It’s referred to as ‘composite literal’

Also, if there is reason to iterate (like calculating the values based loop variable or something) then you could use the range keyword rather than the old school for i is equal to, i is less than, i++ loop.

for i := range onesSlice {
    onesSlice[i] = 1
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)