[talk] Go As a Programming Language

Mark Phelan phelanm at gmail.com
Mon May 22 15:19:27 EDT 2017


* Sujit K M <kmsujit at gmail.com> [2017-05-22 20:29:25+0530]:
> Is there a suggestion for a book on Go?

maybe this so-called-object-relational-mapping thing gorm
can give you a jump start?

# follow: http://jinzhu.me/gorm/
mjp at quirm:~/src/golang/gorm/quickstart$ sudo apt-get install golang
 ..The following NEW packages will be installed:
     golang golang-1.7 golang-1.7-doc golang-1.7-go golang-1.7-src golang-doc
     golang-go golang-src
   0 upgraded, 8 newly installed, 0 to remove and 798 not upgraded..
mjp at quirm:~/src/golang/gorm/quickstart$ go get -u github.com/jinzhu/gorm
 package github.com/jinzhu/gorm: cannot download, $GOPATH not set. For
more details see: go help gopath
mjp at quirm:~/src/golang/gorm/quickstart$ go help gopath
 ...
mjp at quirm:~/src/golang/gorm/quickstart$ export
GOPATH=/home/mjp/src/golang/gorm/quickstart
mjp at quirm:~/src/golang/gorm/quickstart$ go get -u github.com/jinzhu/gorm
mjp at quirm:~/src/golang/gorm/quickstart$ ls |cat
 pkg
 src
mjp at quirm:~/src/golang/gorm/quickstart$ cat qs.go
package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
)

type Product struct {
  gorm.Model
  Code string
  Price uint
}

func main() {
  db, err := gorm.Open("sqlite3", "test.db")
  if err != nil {
    panic("failed to connect database")
  }
  defer db.Close()

  // Migrate the schema
  db.AutoMigrate(&Product{})

  // Create
  db.Create(&Product{Code: "L1212", Price: 1000})

  // Read
  var product Product
  db.First(&product, 1) // find product with id 1
  db.First(&product, "code = ?", "L1212") // find product with code l1212

  // Update - update product's price to 2000
  db.Model(&product).Update("Price", 2000)

  // Delete - delete product
  db.Delete(&product)
}
mjp at quirm:~/src/golang/gorm/quickstart$ go run qs.go
src/github.com/jinzhu/gorm/dialects/sqlite/sqlite.go:3:8: cannot find
package "github.com/mattn/go-sqlite3" in any of:
/usr/lib/go-1.7/src/github.com/mattn/go-sqlite3 (from $GOROOT)
/home/mjp/src/golang/gorm/quickstart/src/github.com/mattn/go-sqlite3
(from $GOPATH)

mjp at quirm:~/src/golang/gorm/quickstart$ go get -u github.com/mattn/go-sqlite3
mjp at quirm:~/src/golang/gorm/quickstart$ go run qs.go
# it works, as in: no errors, test.db exists?
mjp at quirm:~/src/golang/gorm/quickstart$ ls |cat
 pkg
 qs.go
 src
 test.db

On Mon, May 22, 2017 at 2:31 PM, N.J. Thomas <njt at ayvali.org> wrote:
> * Sujit K M <kmsujit at gmail.com> [2017-05-22 20:29:25+0530]:
>> Is there a suggestion for a book on Go?
>
> Just started learning it recently myself. The "Tour of Go" site is pretty
> helpful if you already know how to program:
>
>     https://tour.golang.org/list
>
> The thing that struck me as I started to learn is how different Go is
> from anything else I've ever encountered. It took me about a day to
> learn Python (I had Perl and C under my belt at the time), but Go does
> its own thing (some of it makes sense, other things I'm not so sure
> about).
>
> Thomas
>
> _______________________________________________
> talk mailing list
> talk at lists.nycbug.org
> http://lists.nycbug.org/mailman/listinfo/talk



-- 
Green New Deal, Full Employment, 20 million jobs, 100% Clean Energy:
http://www.jill2016.com/greennewdeal




More information about the talk mailing list