How to use struct from another package?

521 views
Skip to first unread message

Shrimpy Wu

unread,
Jul 23, 2015, 9:36:06 PM7/23/15
to golang-nuts
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"
)

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
}

Roberto Zanotto

unread,
Jul 23, 2015, 9:45:32 PM7/23/15
to golang-nuts, imx...@gmail.com
You can use the struct, you just can't use field "a", because it's lowercase and thus "package private" :)

Kiki Sugiaman

unread,
Jul 23, 2015, 9:47:31 PM7/23/15
to golan...@googlegroups.com
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.
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiki Sugiaman

unread,
Jul 23, 2015, 9:49:01 PM7/23/15
to golan...@googlegroups.com
Totally missed that :)
--

Roberto Zanotto

unread,
Jul 23, 2015, 10:25:19 PM7/23/15
to golang-nuts, imx...@gmail.com
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:

Shrimpy WU

unread,
Jul 23, 2015, 11:28:21 PM7/23/15
to Kiki Sugiaman, golan...@googlegroups.com

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: golan...@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" :)

--
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...@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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages