FAQ
get "unknown TryGo.TestStruct field 'a' in struct literal" error. why i
cannot use struct from another package?

in my source code i have below folder/files structure, and i run command
"go run main/main.go"

src
--main
---main.go

----MyType.go

main.go

package main

import (
   "fmt"
   "github.com/shrimpy/TryGo"
)

func main() {
   testStructInst := TryGo.TestStruct {
     a: "a",
     b: "b",
     c: "c",
   }

   fmt.Println(testStructInst)
}


in MyType.go

package TryGo

type TestStruct struct {
   a string
   b string
   c string
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Roberto Zanotto at Jul 24, 2015 at 1:45 am
    You can use the struct, you just can't use field "a", because it's
    lowercase and thus "package private" :)
    On Friday, July 24, 2015 at 3:36:06 AM UTC+2, Shrimpy Wu wrote:

    get "unknown TryGo.TestStruct field 'a' in struct literal" error. why i
    cannot use struct from another package?

    in my source code i have below folder/files structure, and i run command
    "go run main/main.go"

    src
    --main
    ---main.go

    ----MyType.go

    main.go

    package main

    import (
    "fmt"
    "github.com/shrimpy/TryGo"
    )

    func main() {
    testStructInst := TryGo.TestStruct {
    a: "a",
    b: "b",
    c: "c",
    }

    fmt.Println(testStructInst)
    }


    in MyType.go

    package TryGo

    type TestStruct struct {
    a string
    b string
    c string
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kiki Sugiaman at Jul 24, 2015 at 1:48 am
    Totally missed that :)

    On 24/07/15 11:45, Roberto Zanotto wrote:
    You can use the struct, you just can't use field "a", because it's
    lowercase and thus "package private" :)

    On Friday, July 24, 2015 at 3:36:06 AM UTC+2, Shrimpy Wu wrote:

    get "unknown TryGo.TestStruct field 'a' in struct literal" error.
    why i cannot use struct from another package?

    in my source code i have below folder/files structure, and i run
    command "go run main/main.go"

    src
    --main
    ---main.go

    ----MyType.go

    main.go

    package main

    import (
    "fmt"
    "github.com/shrimpy/TryGo <http://github.com/shrimpy/TryGo>"
    )

    func main() {
    testStructInst := TryGo.TestStruct {
    a: "a",
    b: "b",
    c: "c",
    }

    fmt.Println(testStructInst)
    }


    in MyType.go

    package TryGo

    type TestStruct struct {
    a string
    b string
    c string
    }

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kiki Sugiaman at Jul 24, 2015 at 1:47 am
    You need to first "go get" your own package from github because of the
    way you import it. If you don't want to do that, put MyType.go inside
    its own folder (src/TryGo/) and get rid of github.com/shrimpy/ from the
    import path.

    On 24/07/15 11:36, Shrimpy Wu wrote:
    get "unknown TryGo.TestStruct field 'a' in struct literal" error. why
    i cannot use struct from another package?

    in my source code i have below folder/files structure, and i run
    command "go run main/main.go"

    src
    --main
    ---main.go

    ----MyType.go

    main.go

    package main

    import (
    "fmt"
    "github.com/shrimpy/TryGo"
    )

    func main() {
    testStructInst := TryGo.TestStruct {
    a: "a",
    b: "b",
    c: "c",
    }

    fmt.Println(testStructInst)
    }


    in MyType.go

    package TryGo

    type TestStruct struct {
    a string
    b string
    c string
    }
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Roberto Zanotto at Jul 24, 2015 at 2:25 am
    ksug also has a good point. If your package import path is
    "foo/bar/pkgname", your package should be in "$GOPATH/src/foo/bar/pkgname"
    (no matter if github is involved or not).
    On Friday, July 24, 2015 at 3:36:06 AM UTC+2, Shrimpy Wu wrote:

    get "unknown TryGo.TestStruct field 'a' in struct literal" error. why i
    cannot use struct from another package?

    in my source code i have below folder/files structure, and i run command
    "go run main/main.go"

    src
    --main
    ---main.go

    ----MyType.go

    main.go

    package main

    import (
    "fmt"
    "github.com/shrimpy/TryGo"
    )

    func main() {
    testStructInst := TryGo.TestStruct {
    a: "a",
    b: "b",
    c: "c",
    }

    fmt.Println(testStructInst)
    }


    in MyType.go

    package TryGo

    type TestStruct struct {
    a string
    b string
    c string
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shrimpy WU at Jul 24, 2015 at 3:28 am
    Ah.. Thanks guys! Will change it and try it out.



    Sent from Mobile





    From: Kiki Sugiaman
    Sent: Thursday, July 23, 2015 6:49 PM
    To: golang-nuts@googlegroups.com
    Subject: Re: [go-nuts] Re: How to use struct from another package?


    Totally missed that :)

    On 24/07/15 11:45, Roberto Zanotto wrote:
    You can use the struct, you just can't use field "a", because it's
    lowercase and thus "package private" :)

    On Friday, July 24, 2015 at 3:36:06 AM UTC+2, Shrimpy Wu wrote:

    get "unknown TryGo.TestStruct field 'a' in struct literal" error.
    why i cannot use struct from another package?

    in my source code i have below folder/files structure, and i run
    command "go run main/main.go"

    src
    --main
    ---main.go

    ----MyType.go

    main.go

    package main

    import (
    "fmt"
    "github.com/shrimpy/TryGo <http://github.com/shrimpy/TryGo>"
    )

    func main() {
    testStructInst := TryGo.TestStruct {
    a: "a",
    b: "b",
    c: "c",
    }

    fmt.Println(testStructInst)
    }


    in MyType.go

    package TryGo

    type TestStruct struct {
    a string
    b string
    c string
    }

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/FoMKea_zv3w/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 24, '15 at 1:36a
activeJul 24, '15 at 3:28a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase