Please ignore the paths, they are only for reference. The issue is with the struct import.
Here is my Go code
src/business-logic/foo.go
import (
"fmt"
"strings"
"git.com/common/constants"
)
func someFoo(cloudName string) {
var platform constants.PLATFORM
if cloudName == "azure" {
platform = constants.AZURE
} else if cloudName == "aws" {
platform = constants.AWS
} else if cloudName == "gcp" {
platform = constants.GCP
}
fmt.Sprintf("The cloud is %s", platform)
Here is the git.com/common/constants
package constants
type PLATFORM string
const (
AWS PLATFORM = "aws"
AZURE PLATFORM = "azure"
GCP PLATFORM = "gcp"
)
[EDIT -start-] As requested here is my go.mod
module git.com/folder/folder/my-app
go 1.18
replace git.com/common => /go/src/git.com/common
require (
git.com/common v1.2.0
)
[EDIT -end-]
And when compiled in the gitlab I am getting this error: src/business-logic/foo.go: undefined: constants.GCP
Go version: 1.18
I am very new to the GO and did not find any related answers on the stack overflow.
It looks like you have a module-aware project, and you’re using a replace directive in your go.mod
file to point to a local directory for the git.com/common
package. The error you’re encountering, undefined: constants.GCP
, suggests that the constants
package is not being imported correctly.
Given the project structure and the use of modules, make sure you’re importing the correct path in your foo.go
file. The import path in your foo.go
file should match the module path specified in your go.mod
file.
Here is how you can adjust your import in foo.go
:
import (
"fmt"
"strings"
"git.com/folder/folder/my-app/common/constants"
)
func someFoo(cloudName string) {
var platform constants.PLATFORM
if cloudName == "azure" {
platform = constants.AZURE
} else if cloudName == "aws" {
platform = constants.AWS
} else if cloudName == "gcp" {
platform = constants.GCP
}
fmt.Sprintf("The cloud is %s", platform)
}
Make sure that the import path corresponds to the correct structure within your module.
Additionally, ensure that your constants
package is in the correct location relative to your module root. It should be inside the common
directory within your project structure.
After making these changes, try to build your project again. If the issue persists, check that the constants
package is in the correct module and has the correct export identifiers. The constants.GCP
should be accessible if it’s properly exported from the constants
package.