Create constants visible across packages, accessible directly

lionelmessi

I would like to define my Error Codes in a package models.

error.go

package models

const{
  EOK = iota
  EFAILED
}

How can I use them in another package without referring to them as models.EOK. I would like to use directly as EOK, since these codes would be common across all packages.

Is it the right way to do it? Any better alternatives?

tike

To answer you core question

You can use the dot import syntax to import the exported symbols from another package directly into your package's namespace (godoc):

import . "models"

This way you could directly refer to the EOK constant without prefixing it with models.

However I'd strongly advice against doing so, as it generates rather unreadable code. see below

General/style advice

  1. Don't use unprefixed export path like models. This is considered bad style as it will easily globber. Even for small projects, that are used only internally, use something like myname/models. see goblog
  2. Regarding your question about error generation, there are functions for generating error values, e.g. errors.New (godoc) and fmt.Errorf (godoc). For a general introduction on go and error handling see goblog

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to have a global variable accessible across all packages

From Dev

Ada : operator is not directly visible

From Dev

Logging globally (across packages)

From Dev

Using binary packages directly

From Dev

libxml constants are accessible, yet don't register as defined constants

From Dev

libxml constants are accessible, yet don't register as defined constants

From Dev

How to Declare Always-accessible Constants in Android?

From Dev

React create constants file

From Dev

how to make bootstrap accessible across projects

From Dev

Angularjs: set accessible service across multiple controllers

From Dev

Adding constants to be used across node modules

From Dev

referencing constants and variables across separate class files

From Dev

How to make a page not directly accessible in Flask?

From Dev

Cakephp app/webroot/img folder is directly accessible

From Dev

Accessing variables across packages in Go

From Dev

Sharing structs across multiple packages

From Dev

Usage of go/parser across packages

From Dev

Disk not visible in My computer, still accessible

From Java

is there an easier way to make enum constants visible

From Dev

How do I make macro constants globally accessible in C?

From Dev

MySQL - querying for a result across tables that are not directly linked

From Java

Make variable visible across steps in Bitbucket pipelines?

From Java

Configure Flask dev server to be visible across the network

From Dev

Are private members visible across a partial class?

From Dev

Adding subview and then keep it visible on top across app

From Dev

FlowLayout alignment changes are not directly visible in Swing

From Dev

How to create a String directly?

From Dev

Autowired Spring Beans across Java packages

From Dev

HQL Inner Join across different packages

Related Related

  1. 1

    How to have a global variable accessible across all packages

  2. 2

    Ada : operator is not directly visible

  3. 3

    Logging globally (across packages)

  4. 4

    Using binary packages directly

  5. 5

    libxml constants are accessible, yet don't register as defined constants

  6. 6

    libxml constants are accessible, yet don't register as defined constants

  7. 7

    How to Declare Always-accessible Constants in Android?

  8. 8

    React create constants file

  9. 9

    how to make bootstrap accessible across projects

  10. 10

    Angularjs: set accessible service across multiple controllers

  11. 11

    Adding constants to be used across node modules

  12. 12

    referencing constants and variables across separate class files

  13. 13

    How to make a page not directly accessible in Flask?

  14. 14

    Cakephp app/webroot/img folder is directly accessible

  15. 15

    Accessing variables across packages in Go

  16. 16

    Sharing structs across multiple packages

  17. 17

    Usage of go/parser across packages

  18. 18

    Disk not visible in My computer, still accessible

  19. 19

    is there an easier way to make enum constants visible

  20. 20

    How do I make macro constants globally accessible in C?

  21. 21

    MySQL - querying for a result across tables that are not directly linked

  22. 22

    Make variable visible across steps in Bitbucket pipelines?

  23. 23

    Configure Flask dev server to be visible across the network

  24. 24

    Are private members visible across a partial class?

  25. 25

    Adding subview and then keep it visible on top across app

  26. 26

    FlowLayout alignment changes are not directly visible in Swing

  27. 27

    How to create a String directly?

  28. 28

    Autowired Spring Beans across Java packages

  29. 29

    HQL Inner Join across different packages

HotTag

Archive