0

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?

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • When connecting to a database in Go, you should only choose a database/sql compatible package. Here are some informations : [1](http://stackoverflow.com/questions/11353679/golang-whats-the-recommended-way-to-connect-to-mysql-from-go/11357116#11357116), [2](http://golang.org/pkg/database/sql/) – Denys Séguret Dec 21 '12 at 19:49

3 Answers3

2

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

alex
  • 2,178
  • 16
  • 14
  • I tried to use your package (as well as several others), but it seems Apple might use a different version of the SQL / ODBC C-classes as expected by all these packages. Apple has deprecated a lot of functionality in their headers and because of this Go compiles & runs fail with warnings (e.g. `unix.go:17: warning: 'SQLFreeHandle' is deprecated (declared at /usr/include/sql.h:942)`). I've tried to disable warnings on compile (using `% go build -compiler gccgo -gccgoflags='-Wdeprecated' -x`), but that doesn't work, can't get the compiler to use compiler flags for Apple's classes it seems. – Wolfgang Schreurs Dec 22 '12 at 01:06
  • I did not tried to use my package darwin - I use it on windows and linux. My package wouldn't build on darwin, because unix related files have "+build linux" only. You could try and add "darwin" in there to see, if it helps. – alex Dec 22 '12 at 01:28
  • I also do not understand where did you find unix.go in my package. Also, I use standard Go gc compiler, not gccgo. – alex Dec 22 '12 at 01:29
  • As to the warning you get, I would just ignore it for the time being of possible. – alex Dec 22 '12 at 01:31
  • Well, that was with another project, but with sort of the same warnings. Either way, just figured out I did download the iODBC classes from the project website recently and apparently they're installed in a different location (`/usr/local/iODBC/include`). These classes should work as expected, but I guess I have to let the Go compiler "know" if these classes, perhaps by adding them to my classpath. Trying to figure out how to do this, that might be key to using your project. – Wolfgang Schreurs Dec 22 '12 at 01:40
  • I use cgo with unixODBC and freetds on linux. I didn't need to do anything specific, but to install the software. cgo just find the software. For example, this is detailed output of my install command http://pastebin.com/RVatqLzF Hope it helps any. My package does not use cgo on windows. So you don't need anything to build it on windows, just Go. In fact, you could build windows executable on linux or darwin. Alex – alex Dec 22 '12 at 03:07
1

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.

Ross Light
  • 4,769
  • 1
  • 26
  • 37
0

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.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106