13

I tried to learn Go but I frequently feel frustrating because some basic features that other languages has seems not working in Go. So basically, I would like to use struct type that is define in other file. I was able to use functions except struct type. In main.go,

  package main

  import (
      "list"
  )

  func main() {
      lst := list.NewList(false)         
      lst.Insert(5)
      lst.Insert(7)
      lst.InsertAt(2, 1)
      lst.PrintList()
  }

This works perfectly (and all other functions) as I expect (list is in $GOPATH). In package list, I defined struct as follow:

type LinkedList struct {
    head    *node
    size    int
    isFixed bool
}

I wanted to use this struct in other struct, so I attempted to do something like this,

type SomeType struct {
    lst *LinkedList
}

But unfortunately, I got error that the type LinkedList is not defined. How can I use a struct that is defined in other package?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
REALFREE
  • 4,378
  • 7
  • 40
  • 73
  • 1
    Does [this post](http://stackoverflow.com/a/15051192/2908724) answer your question? – bishop Jan 18 '14 at 01:22
  • Nope. As I said, every other functions works except directly using struct type. And that post is about importing other file, I didn't get any error about finding package. – REALFREE Jan 18 '14 at 01:25
  • As long as the name of the struct has an uppercased first letter it should be accessible using the namespace name like @tobi showed. – Mihai Stancu Jan 18 '14 at 01:34
  • name of struct has to be uppercase? – REALFREE Jan 18 '14 at 01:35
  • @REALFREE Go decides whether something is public or package-private by the case of the first letter of the variable. Which is annoying to people who want crazy variable names, but it's a quite useful convention otherwise. – Cubic Jan 18 '14 at 12:35
  • @Cubic I'd rather design to use keyword something like private and public, which explicitly make it understandable. But its Go's way, I'm buying it xD – REALFREE Jan 19 '14 at 06:52

1 Answers1

24

The LinkedList type is in the list namespace, so change your usage of the type to:

type SomeType struct {
    lst *list.LinkedList
}
tlehman
  • 5,125
  • 2
  • 33
  • 51
  • 1
    @REALFREE, "Effective Go" [provides an answer](http://golang.org/doc/effective_go.html#package-names). Please consider reading this document before seriously dabbling with Go. – kostix Jan 18 '14 at 11:56
  • @kostix well thx I skimmed through it but maybe I missed that part – REALFREE Jan 19 '14 at 06:54
  • @REALFREE I had the same issue when starting, it's hard to memorize all the idiosyncrasies of Go, implicit export semantics is unusual, but now you will remember it forever – tlehman Jan 25 '16 at 19:48
  • 1
    Is there a way to refer to the types imported from a package without the fully-qualified package identifier? I've tried `type T package.T` however, if I have a functions associated with `package.T`, `T.functionname()` will be considered undefined. Any clue on this? – steviesama Jun 06 '16 at 00:31