EN VI

I am new to golang programming, I'm not sure why the results are not what I expected!?

2024-03-12 11:00:04
I am new to golang programming, I'm not sure why the results are not what I expected!
package main

import (
    log "github.com/sirupsen/logrus"
)

func printSlice(slice []int) {
    slice[0] = 11
    log.Infof("sli %o", slice)
}

func main() {
    arr := [...]int{1, 2, 3, 4, 5, 6}
    log.Infof("Arr out %o", arr)
    slice := arr[3:]
    printSlice(slice)
    log.Infof("sli out %o", slice)
    log.Infof("Arr out2 %o", arr)
}

Why the result is:

time="2024-03-12T11:38:44+09:00" level=info msg="Arr out [1 2 3 4 5 6]"
time="2024-03-12T11:38:44+09:00" level=info msg="sli [13 5 6]"
time="2024-03-12T11:38:44+09:00" level=info msg="sli out [13 5 6]"
time="2024-03-12T11:38:44+09:00" level=info msg="Arr out2 [1 2 3 13 5 6]"

I think the result should be:

time="2024-03-12T11:38:44+09:00" level=info msg="Arr out [1 2 3 4 5 6]" 
time="2024-03-12T11:38:44+09:00" level=info msg="sli [11 5 6]"
time="2024-03-12T11:38:44+09:00" level=info msg="sli out [11 5 6]"
time="2024-03-12T11:38:44+09:00" level=info msg="Arr out2 [1 2 3 11 5 6]"

My golang version is 1.18.10

Solution:

As per the docs for fmt:

%o base 8

So you are printing your output in base 8, if you change this to base 10 (%d) you will get the expected results (playground).

time="2009-11-10T23:00:00Z" level=info msg="Arr out [1 2 3 4 5 6]"
time="2009-11-10T23:00:00Z" level=info msg="sli [11 5 6]"
time="2009-11-10T23:00:00Z" level=info msg="sli out [11 5 6]"
time="2009-11-10T23:00:00Z" level=info msg="Arr out2 [1 2 3 11 5 6]"

My golang version is 1.18.10

Note that version 1.18 was released in 2022, I'd suggest moving to a supported release (e.g. 1.22 which was released in February), as per the release policy:

Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release. We fix critical problems, including critical security problems, in supported releases as needed by issuing minor revisions (for example, Go 1.6.1, Go 1.6.2, and so on).

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login