panic: SetUint using value obtained using unexported field










-1















From byte buffer received server, I want to copy struct.



The format of buffer is fixed size bytes as below.



00000000 83 27 48 12 6c 00 00 00 01 02 00 00 01 01 00 02 |.'H.l...........|
00000010 10 01 d2 02 96 49 00 00 00 00 87 d6 12 00 00 00 |.....I..........|
00000020 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 02 01 02 3c 01 01 00 00 00 01 01 01 01 18 10 |....<...........|
00000040 2c 01 90 01 01 6c 07 03 c8 02 01 02 03 9c 0a 0b |,....l..........|
00000050 0c 00 00 00 01 01 00 00 00 00 00 00 00 01 01 01 |................|
00000060 01 01 01 01 01 01 01 01 01 00 01 01 01 00 00 00 |................|


My struct is below.



type HeaderT struct 
magicValue [8]byte
bodyLength [4]byte
bodyVersion [1]byte
...



My implementation is at below.



func onMessageReceived(client MQTT.Client, message MQTT.Message) 
payload := message.Payload()
fmt.Printf("Received message on topic: %snMessage: n%sn", message.Topic(), hex.Dump(payload))

header := HeaderT
err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header) // <-- error occurred at this line
...



My code make panic as below.




panic: reflect: reflect.Value.SetUint using value obtained using
unexported field



goroutine 38 [running]: reflect.flag.mustBeAssignable(0x1a8)
/usr/local/go/src/reflect/value.go:231 +0x1ee reflect.Value.SetUint(0x12540e0, 0xc0001a2000, 0x1a8, 0x83)
/usr/local/go/src/reflect/value.go:1551 +0x2f encoding/binary.(*decoder).value(0xc000148d88, 0x12540e0,
0xc0001a2000, 0x1a8)
/usr/local/go/src/encoding/binary/binary.go:548 +0x7c6 encoding/binary.(*decoder).value(0xc000148d88, 0x125cfc0,
0xc0001a2000, 0x1b1)
/usr/local/go/src/encoding/binary/binary.go:510 +0x104 encoding/binary.(*decoder).value(0xc000148d88, 0x129fa00,
0xc0001a2000, 0x199)
/usr/local/go/src/encoding/binary/binary.go:523 +0x2c5 encoding/binary.Read(0x12fcf80, 0xc00018a150, 0x1300c60, 0x14d76d0,
0x1248040, 0xc0001a2000, 0x0, 0x0)
/usr/local/go/src/encoding/binary/binary.go:248 +0x342 main.onMessageReceived(0x13012a0, 0xc000140000, 0x1300c00,
0xc000192000)











share|improve this question



















  • 5





    panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

    – iHelos
    Nov 14 '18 at 5:48






  • 5





    Capitalize the first letter of your fields.

    – majidarif
    Nov 14 '18 at 5:49















-1















From byte buffer received server, I want to copy struct.



The format of buffer is fixed size bytes as below.



00000000 83 27 48 12 6c 00 00 00 01 02 00 00 01 01 00 02 |.'H.l...........|
00000010 10 01 d2 02 96 49 00 00 00 00 87 d6 12 00 00 00 |.....I..........|
00000020 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 02 01 02 3c 01 01 00 00 00 01 01 01 01 18 10 |....<...........|
00000040 2c 01 90 01 01 6c 07 03 c8 02 01 02 03 9c 0a 0b |,....l..........|
00000050 0c 00 00 00 01 01 00 00 00 00 00 00 00 01 01 01 |................|
00000060 01 01 01 01 01 01 01 01 01 00 01 01 01 00 00 00 |................|


My struct is below.



type HeaderT struct 
magicValue [8]byte
bodyLength [4]byte
bodyVersion [1]byte
...



My implementation is at below.



func onMessageReceived(client MQTT.Client, message MQTT.Message) 
payload := message.Payload()
fmt.Printf("Received message on topic: %snMessage: n%sn", message.Topic(), hex.Dump(payload))

header := HeaderT
err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header) // <-- error occurred at this line
...



My code make panic as below.




panic: reflect: reflect.Value.SetUint using value obtained using
unexported field



goroutine 38 [running]: reflect.flag.mustBeAssignable(0x1a8)
/usr/local/go/src/reflect/value.go:231 +0x1ee reflect.Value.SetUint(0x12540e0, 0xc0001a2000, 0x1a8, 0x83)
/usr/local/go/src/reflect/value.go:1551 +0x2f encoding/binary.(*decoder).value(0xc000148d88, 0x12540e0,
0xc0001a2000, 0x1a8)
/usr/local/go/src/encoding/binary/binary.go:548 +0x7c6 encoding/binary.(*decoder).value(0xc000148d88, 0x125cfc0,
0xc0001a2000, 0x1b1)
/usr/local/go/src/encoding/binary/binary.go:510 +0x104 encoding/binary.(*decoder).value(0xc000148d88, 0x129fa00,
0xc0001a2000, 0x199)
/usr/local/go/src/encoding/binary/binary.go:523 +0x2c5 encoding/binary.Read(0x12fcf80, 0xc00018a150, 0x1300c60, 0x14d76d0,
0x1248040, 0xc0001a2000, 0x0, 0x0)
/usr/local/go/src/encoding/binary/binary.go:248 +0x342 main.onMessageReceived(0x13012a0, 0xc000140000, 0x1300c00,
0xc000192000)











share|improve this question



















  • 5





    panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

    – iHelos
    Nov 14 '18 at 5:48






  • 5





    Capitalize the first letter of your fields.

    – majidarif
    Nov 14 '18 at 5:49













-1












-1








-1








From byte buffer received server, I want to copy struct.



The format of buffer is fixed size bytes as below.



00000000 83 27 48 12 6c 00 00 00 01 02 00 00 01 01 00 02 |.'H.l...........|
00000010 10 01 d2 02 96 49 00 00 00 00 87 d6 12 00 00 00 |.....I..........|
00000020 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 02 01 02 3c 01 01 00 00 00 01 01 01 01 18 10 |....<...........|
00000040 2c 01 90 01 01 6c 07 03 c8 02 01 02 03 9c 0a 0b |,....l..........|
00000050 0c 00 00 00 01 01 00 00 00 00 00 00 00 01 01 01 |................|
00000060 01 01 01 01 01 01 01 01 01 00 01 01 01 00 00 00 |................|


My struct is below.



type HeaderT struct 
magicValue [8]byte
bodyLength [4]byte
bodyVersion [1]byte
...



My implementation is at below.



func onMessageReceived(client MQTT.Client, message MQTT.Message) 
payload := message.Payload()
fmt.Printf("Received message on topic: %snMessage: n%sn", message.Topic(), hex.Dump(payload))

header := HeaderT
err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header) // <-- error occurred at this line
...



My code make panic as below.




panic: reflect: reflect.Value.SetUint using value obtained using
unexported field



goroutine 38 [running]: reflect.flag.mustBeAssignable(0x1a8)
/usr/local/go/src/reflect/value.go:231 +0x1ee reflect.Value.SetUint(0x12540e0, 0xc0001a2000, 0x1a8, 0x83)
/usr/local/go/src/reflect/value.go:1551 +0x2f encoding/binary.(*decoder).value(0xc000148d88, 0x12540e0,
0xc0001a2000, 0x1a8)
/usr/local/go/src/encoding/binary/binary.go:548 +0x7c6 encoding/binary.(*decoder).value(0xc000148d88, 0x125cfc0,
0xc0001a2000, 0x1b1)
/usr/local/go/src/encoding/binary/binary.go:510 +0x104 encoding/binary.(*decoder).value(0xc000148d88, 0x129fa00,
0xc0001a2000, 0x199)
/usr/local/go/src/encoding/binary/binary.go:523 +0x2c5 encoding/binary.Read(0x12fcf80, 0xc00018a150, 0x1300c60, 0x14d76d0,
0x1248040, 0xc0001a2000, 0x0, 0x0)
/usr/local/go/src/encoding/binary/binary.go:248 +0x342 main.onMessageReceived(0x13012a0, 0xc000140000, 0x1300c00,
0xc000192000)











share|improve this question
















From byte buffer received server, I want to copy struct.



The format of buffer is fixed size bytes as below.



00000000 83 27 48 12 6c 00 00 00 01 02 00 00 01 01 00 02 |.'H.l...........|
00000010 10 01 d2 02 96 49 00 00 00 00 87 d6 12 00 00 00 |.....I..........|
00000020 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 02 01 02 3c 01 01 00 00 00 01 01 01 01 18 10 |....<...........|
00000040 2c 01 90 01 01 6c 07 03 c8 02 01 02 03 9c 0a 0b |,....l..........|
00000050 0c 00 00 00 01 01 00 00 00 00 00 00 00 01 01 01 |................|
00000060 01 01 01 01 01 01 01 01 01 00 01 01 01 00 00 00 |................|


My struct is below.



type HeaderT struct 
magicValue [8]byte
bodyLength [4]byte
bodyVersion [1]byte
...



My implementation is at below.



func onMessageReceived(client MQTT.Client, message MQTT.Message) 
payload := message.Payload()
fmt.Printf("Received message on topic: %snMessage: n%sn", message.Topic(), hex.Dump(payload))

header := HeaderT
err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header) // <-- error occurred at this line
...



My code make panic as below.




panic: reflect: reflect.Value.SetUint using value obtained using
unexported field



goroutine 38 [running]: reflect.flag.mustBeAssignable(0x1a8)
/usr/local/go/src/reflect/value.go:231 +0x1ee reflect.Value.SetUint(0x12540e0, 0xc0001a2000, 0x1a8, 0x83)
/usr/local/go/src/reflect/value.go:1551 +0x2f encoding/binary.(*decoder).value(0xc000148d88, 0x12540e0,
0xc0001a2000, 0x1a8)
/usr/local/go/src/encoding/binary/binary.go:548 +0x7c6 encoding/binary.(*decoder).value(0xc000148d88, 0x125cfc0,
0xc0001a2000, 0x1b1)
/usr/local/go/src/encoding/binary/binary.go:510 +0x104 encoding/binary.(*decoder).value(0xc000148d88, 0x129fa00,
0xc0001a2000, 0x199)
/usr/local/go/src/encoding/binary/binary.go:523 +0x2c5 encoding/binary.Read(0x12fcf80, 0xc00018a150, 0x1300c60, 0x14d76d0,
0x1248040, 0xc0001a2000, 0x0, 0x0)
/usr/local/go/src/encoding/binary/binary.go:248 +0x342 main.onMessageReceived(0x13012a0, 0xc000140000, 0x1300c00,
0xc000192000)








arrays go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 8:54









noamt

4,13212241




4,13212241










asked Nov 14 '18 at 5:45









sungyongsungyong

4571424




4571424







  • 5





    panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

    – iHelos
    Nov 14 '18 at 5:48






  • 5





    Capitalize the first letter of your fields.

    – majidarif
    Nov 14 '18 at 5:49












  • 5





    panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

    – iHelos
    Nov 14 '18 at 5:48






  • 5





    Capitalize the first letter of your fields.

    – majidarif
    Nov 14 '18 at 5:49







5




5





panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

– iHelos
Nov 14 '18 at 5:48





panic: reflect: reflect.Value.SetUint using value obtained using unexported field: try to make your fields public (like magicValue [8]byte -> MagicValue [8]byte)

– iHelos
Nov 14 '18 at 5:48




5




5





Capitalize the first letter of your fields.

– majidarif
Nov 14 '18 at 5:49





Capitalize the first letter of your fields.

– majidarif
Nov 14 '18 at 5:49












1 Answer
1






active

oldest

votes


















4














The issue is that none of HeaderT's fields are "public".



Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.



From the spec:




Exported identifiers



An identifier may be exported to permit access to it from another package. An identifier is exported if both:



  • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and

  • the identifier is declared in the package block or it is a field name or method name.

All other identifiers are not exported.




Try Exporting them by capitalizing their names:



type HeaderT struct 
MagicValue [8]byte
BodyLength [4]byte
BodyVersion [1]byte
...






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293862%2fpanic-setuint-using-value-obtained-using-unexported-field%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    The issue is that none of HeaderT's fields are "public".



    Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.



    From the spec:




    Exported identifiers



    An identifier may be exported to permit access to it from another package. An identifier is exported if both:



    • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and

    • the identifier is declared in the package block or it is a field name or method name.

    All other identifiers are not exported.




    Try Exporting them by capitalizing their names:



    type HeaderT struct 
    MagicValue [8]byte
    BodyLength [4]byte
    BodyVersion [1]byte
    ...






    share|improve this answer



























      4














      The issue is that none of HeaderT's fields are "public".



      Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.



      From the spec:




      Exported identifiers



      An identifier may be exported to permit access to it from another package. An identifier is exported if both:



      • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and

      • the identifier is declared in the package block or it is a field name or method name.

      All other identifiers are not exported.




      Try Exporting them by capitalizing their names:



      type HeaderT struct 
      MagicValue [8]byte
      BodyLength [4]byte
      BodyVersion [1]byte
      ...






      share|improve this answer

























        4












        4








        4







        The issue is that none of HeaderT's fields are "public".



        Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.



        From the spec:




        Exported identifiers



        An identifier may be exported to permit access to it from another package. An identifier is exported if both:



        • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and

        • the identifier is declared in the package block or it is a field name or method name.

        All other identifiers are not exported.




        Try Exporting them by capitalizing their names:



        type HeaderT struct 
        MagicValue [8]byte
        BodyLength [4]byte
        BodyVersion [1]byte
        ...






        share|improve this answer













        The issue is that none of HeaderT's fields are "public".



        Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.



        From the spec:




        Exported identifiers



        An identifier may be exported to permit access to it from another package. An identifier is exported if both:



        • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and

        • the identifier is declared in the package block or it is a field name or method name.

        All other identifiers are not exported.




        Try Exporting them by capitalizing their names:



        type HeaderT struct 
        MagicValue [8]byte
        BodyLength [4]byte
        BodyVersion [1]byte
        ...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 8:25









        noamtnoamt

        4,13212241




        4,13212241





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293862%2fpanic-setuint-using-value-obtained-using-unexported-field%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands