在尝试使用Go语言中的一个包时出现错误 – 需要帮助修复。

huangapple go评论81阅读模式
英文:

Errors when trying to use a package in Go - need help to fix

问题

首先,我对Go语言还不熟悉。

我决定创建一个Go版本的一些我们作为业余项目开发的.NET服务。尝试这样做的原因是:

  • 我对Go语言有些好奇。
  • 我认为.NET的Web服务可以有很大的改进空间,但我不是我们公司的.NET开发人员。

现在,为了创建这些Web服务,我需要访问我们的数据库。我配置了我们的测试服务器以允许使用ODBC访问数据库。

下一步:在Go应用程序中使ODBC连接工作。有几个Go的ODBC包可用,目前我正在尝试使用这个:https://github.com/BenoyRNair/godbc

项目中包含了一个示例。当我尝试运行示例时,我得到了以下错误信息:

MacBook-Air:go wolf$ go run example.go 
# godbc
godbc.go:77:2: expected declaration, found '('
godbc.go:81:2: expected declaration, found 'IDENT' x
godbc.go:104:2: expected declaration, found 'IDENT' dsn
godbc.go:109:2: expected declaration, found 'IDENT' returnInt
godbc.go:132:2: expected declaration, found 'IDENT' driver
godbc.go:137:2: expected declaration, found 'IDENT' returnInt
godbc.go:161:2: expected declaration, found 'IDENT' outConnectionString
godbc.go:185:2: expected declaration, found 'IDENT' messageText
godbc.go:188:2: expected declaration, found 'IDENT' returnInt
godbc.go:212:2: expected declaration, found 'IDENT' returnInt
godbc.go:230:2: expected declaration, found 'IDENT' returnInt
godbc.go:242:2: expected declaration, found 'IDENT' returnInt
godbc.go:275:2: expected declaration, found 'IDENT' returnInt
godbc.go:291:2: expected declaration, found 'IDENT' buffer
godbc.go:336:2: expected declaration, found 'for'

现在我不确定如何处理这个错误。我认为这个错误可能与文件的格式有关,但也可能是其他原因。当我查看github页面上的问题选项卡时,我注意到有人提到了同样的问题,但指出该代码在最新版本的Go中无法编译,所以我认为它过去是可以编译的。

有人知道是否有一些简单的解决方法来解决这个问题,或者我应该尝试其他的ODBC包?如果是这样的话,有什么包推荐吗?

英文:

First of all, I'm new to the Go language.

I've decided to create a Go version of some of the .NET services we developed as a hobby project. Reasons to attempt this are:

  • I've been somewhat intrigued by the Go language.
  • I think the .NET webservices could be improved a lot, but I'm not the .NET developer at our company.

Now in order to create these webservices, I need to gain access to our database. I configured our test server to allow access to the database using ODBC.

Next step: get an ODBC connection working in a Go application. There are several Go ODBC packages available, currently I'm trying to use this one: https://github.com/BenoyRNair/godbc

Included in the project is an example. When I try to run the example, I get the following error messages:

MacBook-Air:go wolf$ go run example.go 
# godbc
godbc.go:77:2: expected declaration, found '('
godbc.go:81:2: expected declaration, found 'IDENT' x
godbc.go:104:2: expected declaration, found 'IDENT' dsn
godbc.go:109:2: expected declaration, found 'IDENT' returnInt
godbc.go:132:2: expected declaration, found 'IDENT' driver
godbc.go:137:2: expected declaration, found 'IDENT' returnInt
godbc.go:161:2: expected declaration, found 'IDENT' outConnectionString
godbc.go:185:2: expected declaration, found 'IDENT' messageText
godbc.go:188:2: expected declaration, found 'IDENT' returnInt
godbc.go:212:2: expected declaration, found 'IDENT' returnInt
godbc.go:230:2: expected declaration, found 'IDENT' returnInt
godbc.go:242:2: expected declaration, found 'IDENT' returnInt
godbc.go:275:2: expected declaration, found 'IDENT' returnInt
godbc.go:291:2: expected declaration, found 'IDENT' buffer
godbc.go:336:2: expected declaration, found 'for'

Now I'm not sure how to deal with this error. I'm thinking the error might have something to do with how the file is formatted, but perhaps something else is at hand. When I look at the github page, on the issues tab, I've noticed someone else mentioning the same issue, but noted that the code doesn't compile with the latest version of Go, so I assume it has compiled in the past.

Does anyone know if there is some easy fix for this issue or should I try my luck with some other ODBC package? And if so: what package would be recommended?

答案1

得分: 2

https://github.com/BenoyRNair/godbc是一个非常旧的包。正如你发现的那样,它甚至无法编译。

现在,Go语言有一个标准的database/sql包,用于访问任何SQL数据库。它提供了接口,但你还需要一个“驱动程序包”,该包将实现对你选择的数据库的访问。这种设计的思想是你可以替换“驱动程序包”以访问不同的数据库。除非你需要一些ODBC特定的功能,否则你应该尝试使用database/sql包。这将允许你尝试不同的驱动程序,直到找到适合你的那个。

我自己编写了一个ODBC驱动程序http://code.google.com/p/odbc/。我用它来访问MS SQL Server。也许它对你也有用。

Alex

英文:

https://github.com/BenoyRNair/godbc is very old package. It does not even compile anymore, as you have discovered.

Go has database/sql standard package now to access any sql database. It provides interface, but you will also need a "driver package" that will implement access to your selected database. The idea of this design is that you could replace "driver package" to access different database. Most, if not all, of your user code should not change.

Unless you need some ODBC specific functionality, you should try and use database/sql package. This will allow you to try different drivers until you find the one that works for you.

I have written ODBC driver myself http://code.google.com/p/odbc/. I use it to access MS SQL Server. Perhaps, it will work for you.

Alex

答案2

得分: 1

看起来这段代码是在Go语言还没有自动分号插入规则之前编写的,即使现在使用gofix也不会处理这样的代码。我对ODBC不太了解,但我认为你最好使用一个不同的包。

英文:

Looks like that code was written before Go had the automatic semicolon insertion rules, even gofix won't touch code like that nowadays. I don't know much about ODBC, but I think you'll be better off with a different package.

答案3

得分: 0

大多数使用Microsoft SQL Server的Go用户似乎都使用github.com/denisenkom/go-mssqldb驱动程序包以及标准的database/sql包。

您或其他任何人可能会从我的答案中获得帮助,该答案解释了我为使演示程序正常工作而经历的一些情况。我必须进行了几个SQL Server配置更改。

 - 启用SQL Server身份验证和Windows身份验证
 - 启用TCP连接
 - 创建SQL Server用户并分配必要的权限

该示例适用于在本地机器上安装为默认实例的SQL Server。

英文:

Most people using Go with Microsoft SQL Server seem to be using the driver package github.com/denisenkom/go-mssqldb along with the standard database/sql package.

You or anyone else may be helped by my answer to Go with SQL Server driver is unable to connect successfully, login fail which details some of what I went through to make a demo program work. There are several SQL Server configuration changes I had to make.

 - enable SQL Server Authentication as well as Windows Authentication
 - enable TCP connections
 - create SQL Server user and assign necessary permissions

That example works with SQL Server installed as a default instance on the local machine.

huangapple
  • 本文由 发表于 2012年12月22日 02:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/13995550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定