dongrong5189 2018-09-07 13:01
浏览 34
已采纳

如何制作自己的枚举

Im new in GoLang and im looking for some help. Im working with Windows 10 and Visual Studio Code.

What i want to do is to use Enums in my main.go file. I make a folder for it named "Enums", and file in it named SQLQuerys.go that looks like this:

package SqlQuerys

type SqlQuery string

const (

CreateTable SqlQuery = `CREATE TABELE key.users(id int, email text, title text, content text, magic_number int, PRIMARY KEY(id));`

)

So it is simple string that i want to pass in function in main.go that looks like this (i commented query that works fine):

package main

import (
    "log"

    SqlQuerys "golangapi/Enums" //import enums here

    "github.com/gocql/gocql"
)

func main() {
    // connect to the cluster
    cluster := gocql.NewCluster("127.0.0.1")
    cluster.ProtoVersion = 3
    cluster.Keyspace = "key"
    cluster.Consistency = gocql.Quorum
    session, _ := cluster.CreateSession()
    defer session.Close()

    //if err := session.Query(`CREATE TABEL IF NOT EXISTS key.users(id int, email text, title text, content text, magic_number int, PRIMARY KEY(id));`).Exec(); err != nil {
    //  log.Fatal(err)
    //}

    if err := session.Query(SqlQuerys.CreateTable).Exec(); err != nil {
        log.Fatal(err)
    }

}

How to import one GoLang file to another?

After debug, i get this error:

main.go:6:2: cannot find package "golangapi/Enums" in any of:
C:\Go\src\golangapi\Enums (from $GOROOT)
C:\Users\Admin\go\src\golangapi\Enums (from $GOPATH)
exit status 1

How to make possible to see another .go file in another? Thanks for any advices

  • 写回答

1条回答 默认 最新

  • duanjiong5686 2018-09-07 13:55
    关注

    Go is more like idiomatic Java. While in Java technically you can mix and match the namespace declarations in files with the directory structure, generally they need to match. In Go, they must match in order to work correctly. Your import path (used in an import statement) must match either a repo the library can be checked out from, or the path on disk starting from $GOPATH/src (which should generally be one and the same). The package name (used in the package statement) should match the last part of the path (the name of the directory containing the file). There is no referencing from one file to another file, only from one file to a package (just like Java). So, to take your example:

    $GOPATH
     - src/
       - golangapi/
         - main.go
         - enums/
           - sqlqueries.go
    

    main.go:

    package main
    
    import (
        "log"
        "golangapi/enums"
        "github.com/gocql/gocql"
    )
    ...
    if err := session.Query(enums.CreateTable).Exec(); err != nil {
        log.Fatal(err)
    }
    

    sqlqueries.go:

    package enums
    
    type SqlQuery string
    
    const (
    
    CreateTable SqlQuery = `CREATE TABELE key.users(id int, email text, title text, content text, magic_number int, PRIMARY KEY(id));`
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看