Home > Backend Development > Golang > How Do Go Modules Handle Internal Packages to Prevent External Imports?

How Do Go Modules Handle Internal Packages to Prevent External Imports?

Patricia Arquette
Release: 2024-12-10 04:44:09
Original
476 people have browsed it

How Do Go Modules Handle Internal Packages to Prevent External Imports?

Organization of Go Code with Internal Packages

Understanding Internal Packages

Internal packages, denoted by the "internal" directory, are intended for grouping related code that should not be exposed outside the project. In Go modules, internal packages cannot be imported by external projects.

Module Structure

In the provided example, the project structure is as follows:

project/
  internal/
    foo/
      foo.go  # package foo
    bar/
      bar.go  # package bar
  main.go
Copy after login

where main.go imports the internal packages project/internal/foo and project/internal/bar.

Import Issue

The issue you encountered was trying to import internal packages from outside the project directory. Internal packages can only be imported from within the same project, so using the paths project/internal/foo and project/internal/bar from main.go would not work.

Solution with Go Modules

However, with the introduction of Go modules in Go v1.11, it's no longer necessary to specify your project path in $GOPATH/src. Create a go.mod file and define the internal package modules as follows:

project/
  go.mod
  main.go
  
\---internal
    +---bar
    |       bar.go
    |       go.mod
    |
    \---foo
            foo.go
            go.mod
Copy after login
project/internal/bar/go.mod
Copy after login
module bar

go 1.14
Copy after login
project/internal/bar/bar.go
Copy after login
package bar

import "fmt"

// Bar prints "Hello from Bar"
func Bar() {
    fmt.Println("Hello from Bar")
}
Copy after login
project/internal/foo/go.mod
Copy after login
module foo

go 1.14
Copy after login
project/internal/foo/foo.go
Copy after login
package foo

import "fmt"

// Foo prints "Hello from Foo"
func Foo() {
    fmt.Println("Hello from Foo")
}
Copy after login
project/main.go
Copy after login
package main

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

func main() {
    bar.Bar()
    foo.Foo()
}
Copy after login
project/go.mod
Copy after login
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
Copy after login

Key Points

  • Internal packages are restricted to use within the same project.
  • Go modules provide a way to structure projects with internal packages using go.mod files and replace directives.
  • Versioning in require and replace does not affect the functionality, as long as the package is found in the correct location.

The above is the detailed content of How Do Go Modules Handle Internal Packages to Prevent External Imports?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template