How to use "internal" packages?

how to use semicolon
how to use a colon
when to use a dash
when to use a semicolon examples
how to use in a sentence
when to use a colon in a sentence
when to use a semicolon in a list
when to use a comma

I try understand how to organize go code using "internal" packages. Let me show what the structure I have:

project/
  internal/
    foo/
      foo.go # package foo
    bar/
      bar.go # package bar
  main.go

# here is the code from main.go
package main

import (
  "project/internal/foo"
  "project/internal/bar"
)

project/ is outside from GOPATH tree. Whatever path I try to import from main.go nothing works, the only case working fine is import "./internal/foo|bar". I think I do something wrong or get "internal" package idea wrong in general. Could anybody make things clearer, please?

UPDATE

The example above is correct the only what I need was to place project/ folder under $GOPATH/src. So the thing is import path like the project/internal/foo|bar is workable if we only import it from project/ subtree and not from the outside.

The packages have to be located in your $GOPATH in order to be imported. The example you gave with import "./internal/foo|bar" works because it does a local-import. internal only makes it so code that doesn't share a common root directory to your internal directory can't import the packages within internal.

If you put all this in your gopath then tried to import from a different location like OuterFolder/project2/main.go where OuterFolder contains both project and project2 then import "../../project/internal/foo" would fail. It would also fail as import "foo" or any other way your tried due to not satisfying this condition;

An import of a path containing the element "internal" is disallowed if the importing code is outside the tree rooted at the parent of the "internal" directory.

Now if you had the path $GOPATH/src/project then you could do import "foo" and import "bar" from within $GOPATH/src/project/main.go and the import would succeed. Things that are not contained underneath project however would not be able to import foo or bar.

Semicolons: A Quick Guide How to Use a Semicolon, Semicolons are as basic as a period stacked on top of a comma. Does that mean you can use it like either one? Here are the rules for using semicolons correctly� How to Use a Semicolon Correctly. 1. Semicolons Connect Related Independent Clauses. You can use a semicolon to join two closely related independent clauses. Let’s put that 2. Delete the Conjunction When You Use a Semicolon. 3. Use Semicolons in a Serial List. 4. Use Semicolons With Conjunctive

below way is more scalable, especially when you plan to build multiple binaries

github.com/servi-io/api
├── cmd/
│   ├── servi/
│   │   ├── cmdupdate/
│   │   ├── cmdquery/
│   │   └── main.go
│   └── servid/
│       ├── routes/
│       │   └── handlers/
│       ├── tests/
│       └── main.go
├── internal/
│   ├── attachments/
│   ├── locations/
│   ├── orders/
│   │   ├── customers/
│   │   ├── items/
│   │   ├── tags/
│   │   └── orders.go
│   ├── registrations/
│   └── platform/
│       ├── crypto/
│       ├── mongo/
│       └── json/

The folders inside cmd/ represents the number of binaries you want to build.

for more

Using Semicolons – The Writing Center – UW–Madison, Use a semicolon between two independent clauses that are connected by conjunctive adverbs or transitional phrases. However they choose to write, people are� How to Use Windows 10. By Avram Piltch - Online Editorial Director 17 June 2019. Shares. Windows 10 is the most powerful operating system that Microsoft has ever made, but it's also the most

With modules introduction in Go v1.11 and above you don't have to specify your project path in $GOPATH/src

You need to tell Go about where each module located by creating go.mod file. Please refer to go help mod documentation.

Here is an example of how to do it:

project
|   go.mod
|   main.go
|
\---internal
    +---bar
    |       bar.go
    |       go.mod
    |
    \---foo
            foo.go
            go.mod

project/internal/bar/go.mod

module bar

go 1.14

project/internal/bar/bar.go

package bar

import "fmt"

//Bar prints "Hello from Bar"
func Bar() {
    fmt.Println("Hello from Bar")
}

project/internal/foo/go.mod

module foo

go 1.14

project/internal/foo/foo.go

package foo

import "fmt"

//Foo prints "Hello from Foo"
func Foo() {
    fmt.Println("Hello from Foo")
}

project/main.go

package main

import (
    "internal/bar"
    "internal/foo"
)

func main() {
    bar.Bar()
    foo.Foo()
}

Now the most important module project/go.mod

module project

go 1.14


require internal/bar v1.0.0
replace internal/bar => ./internal/bar
require internal/foo v1.0.0
replace internal/foo => ./internal/foo

Couple things here:

  1. You can have any name in require. You can have project/internal/bar if you wish. What Go think it is URL address for the package, so it will try to pull it from web and give you error
go: internal/bar@v1.0.0: malformed module path "internal/bar": missing dot in first path element

That is the reason why you need to have replace where you tell Go where to find it, and that is the key!

replace internal/bar => ./internal/bar
  1. The version doesn't matter in this case. You can have v0.0.0 and it will work.

Now, when you execute your code you will have

Hello from Bar
Hello from Foo

Here is GitHub link for this code example

When to Use Commas, Colons, Semicolons, and Dashes, Which Punctuation Mark Should I Use? Knowing which punctuation to apply within sentences can be confusing. But rules are important and made to be followed� Once you’re logged in you can use the Ask a question function and manage personnel. Asking a question There’s a virtual assistant on your Business Hub homepage, where you can ask a question.

Also to check: When you are using your externally imported object types: make sure you are prefixing them with the namespace they're in. As a golang newbie, I didn't know I had to do that, and was wondering why is VS Code just removing my import (for not being used) when I saved. It's because I had to prefix the imported object with the namespace name:

Example:
import (
    "myInternalPackageName"  // works fine as long as you follow all instructions in this thread
)

//Usage in code: 
myInternalPackageName.TheStructName   // prefix it, or it won't work. 

If you don't put the namespace prefix before the object/struct name, VS code just removes your import for being unused, and then you still have the error: "Can't find TheStructName"... That was very confusing, and I had to do a build without VS code through the command line to understand that.

The point is: I had to specify the package prefix when actually using the struct from that internal package.

If you don't want to use the qualifier prefix when using imported objects, use:

import . "thePath"    // Can use contents without prefixing. 

Reference: What does the '.' (dot or period) in a Go import statement do?

Semicolons, Use a semicolon in place of a period to separate two sentences where the conjunction has been left out. How To Use Excel: A Beginner's Guide To Getting Started 1: Grab your free exercise file here!. 2: Opening a spreadsheet. 3: Working with the Ribbon. 4: Managing your worksheets. 5: Entering data. 6: Basic calculations. 7: Unlocking the power of functions. 8: Saving and sharing your work. 9:

How to use a semicolon (;), The road runs through a beautiful wooded valley; the railway line follows it. An art director searched North Africa; I went to the Canary Islands. You can also use a� How to use a semicolon. The most feared punctuation on earth. More grammar comics from The Oatmeal. Thanks to Library Lady Jane for all her help in writing these grammar guides over the years. If you would like a regular serving of grammar-related awesom

How to use a semicolon - Emma Bryce, View full lesson: http://ed.ted.com/lessons/how-to-use-a-semicolon-emma-bryce It may seem Duration: 3:36 Posted: Jul 6, 2015 How to Use "That" and "Which" Correctly. It can be hard to know when to use "which" and when to use "that" in a sentence, even if you are a native English speaker. Knowing the difference between a restrictive and non-restrictive clause and

How to use the SEMI-COLON in English writing, Watch this important lesson and learn all about the semi-colon. I explain exactly what this Duration: 18:24 Posted: Nov 1, 2019 Use the laptop's pointing device. On most computers, this is a flat, touch-sensitive area called a touchpad which will allow you to use your finger as your mouse. Simply slide one finger on the touchpad area to move the cursor.

Comments
  • You can't import packages that are not in $GOPATH/src. Try creating a project/baz package, you won't be able to import it either. Put all your projects into GOPATH.
  • What if I don't want to place every project under GOPATH?
  • That's how the go tools work. If you want to use a package, it needs to be under GOPATH.
  • There is no "I want" with the go tooling: Use the tools like they are meant to be used; everything else will lead to pain.
  • If you don't want to place your source code under $GOPATH, you can create a symbolic link with the ln command to your source directory, if you're on *UNIX (mainly Mac OS X or Linux). If you're on Windows, use the mklink command which is a built-in of the shell.