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));`
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来