golang 去文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 去文件相关的知识,希望对你有一定的参考价值。

# Raw Sql query to fetch mysql table row count using beego ORM

import package beego orm

```
package main

import "github.com/astaxie/beego/orm"
```

We have to initialize the mysql connection, for that we have to import the mysql dapter for golang

add `_ "github.com/go-sql-driver/mysql"` to the import statement

let us initialize the connection

```
func init() {
  orm.RegisterDriver("mysql", orm.DR_MySQL)
  orm.RegisterDataBase("default", "mysql", "mysql_username:mysql_password@/db_name?charset=utf8")
}
```

We register the mysql Adapter here,
and then Register Database

Couple of things to note,
1. We need to have a "default" connection,
2. database has to be specified again, "here it is "mysql"
3. "mysql_username:mysql_password@/db_name" replace them with your credentials. You are running it on local machine and the charset is utf8 is my assumtion (just keeping the scope limited)


Lets say you have a Table called Visits with columns id, name

So we create a struct, thwt will act as the Model for the table

```
type Visits struct {
  Id int
  Name string
}

```

Now we have to register the Model to the ORM

in init function, we add `orm.RegisterModel(new(Visits))` after the existing lines of code.

Time to fetch some data,

create your main function

```
function main() {
  o := orm.NewOrm()
  o.Using("default")
}
```

you create an ORM instance,
tell it which config to use. We have created a default one.
This means you can define multiple databases

The query we want data from is

```
select count(*) as Count from visits where name  = 'name';
```

and the return has to be an integer value, the count

lets create the call, and pass the value for name as a parameter, so we can show some runtime control over the query.

```
  var count int
  o.Raw("select count(*) as Count from visits where name = ?", 'plant').QueryRow(&count)
```

add this to your main function
and print the value, you will have got it!

your whole code look something like this

```
package main

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	orm.RegisterDriver("mysql", orm.DR_MySQL)
	orm.RegisterDataBase("default", "mysql", "mysql_username:mysql_password@/database_name?charset=utf8")

	orm.RegisterModel(new(Visits))
}

type Visits struct {
	Id   int
	Name string
}

func main() {
	o := orm.NewOrm()
	o.Using("default")

	var count int
	o.Raw("select count(*) as Count from visits where name = ?", "1").QueryRow(&count)

	fmt.Println(count)
}
```

get the code here: ```https://gist.github.com/sumitasok/bfaa8a7194d3532878e9#file-orm_raw_query_integer-go```

couple of things,
for using beego/orm you have to get it. Run 
` go get github.com/astaxie/beego ` on your console

and to use println, import fmt package.


package main

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	orm.RegisterDriver("mysql", orm.DR_MySQL)
	orm.RegisterDataBase("default", "mysql", "mysql_username:mysql_password@/database_name?charset=utf8")

	orm.RegisterModel(new(Tickets))
}

type Tickets struct {
	Id               int
	event_identifier string
}

func main() {
	o := orm.NewOrm()
	o.Using("default")

	var count int
	o.Raw("select count(*) as Count from tickets where event_identifier = ?", "1").QueryRow(&count)

	fmt.Println(count)
}
package main

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	orm.RegisterDriver("mysql", orm.DR_MySQL)
	orm.RegisterDataBase("default", "mysql", "root:@/mts_dev?charset=utf8")

}

func main() {
	o := orm.NewOrm()
	o.Using("default")

	o.Raw("BEGIN").Exec()

	if _, err1 := o.Raw("UPDATE tickets set booked_by = 12345 where id = 1").Exec(); err1 != nil {
		fmt.Println("Error 1", err1)
	} else {
		if _, err2 := o.Raw("UPDATE tickets set locked_by = '45440000_q' where id = 15").Exec(); err2 != nil {
			fmt.Println("Error 2", err2)
		} else {
			o.Raw("COMMIT").Exec()
		}
	}
}
package main

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	orm.RegisterDriver("mysql", orm.DR_MySQL)
	orm.RegisterDataBase("default", "mysql", "mysql_username:mysql_password@/database_name?charset=utf8")
}

func main() {
	o := orm.NewOrm()
	o.Using("default")
}

golang单元测试

使用testing进行单元测试

  1. golang的测试库testing

  2. 测试文件与被测试文件在同一个包中

  3. 测试文件名为被测试文件名(去后缀)_test.go

  4. 测试用例函数以Test开头,TestFunc1(t *testing.T)

  5. 一个测试用例文件中可以有多个测试用例函数

在测试用例函数中调用被测试函数,根据函数返回结果与预期的正确结果的比较,

判断函数是否正确。t.Log输出日志,t.Fatal输出错误信息。

执行测试文件的方式

  • 执行全部测试文件中全部函数 go test 如果需要输出日志信息go test -v

  • 测试单个文件 go test -v xxx_test.go xxx.go
  • 测试单个方法 go test -v -test.run TestFunc1

// 测试文件  bt_test.go
package goo

import "testing"

func TestFunc1(t *testing.T) {
    result := Addto(100)
    if result == 5050 {
        t.Log("结果正确")
    } else {
        t.Fatal("结果有误")
    }
}
// 被测试文件  bt.go
package goo

func Func1(n int) int {
    if n == 1 {
        return 1
    } else if n <= 0 {
        return -1
    } else {
        return Func1(n-1) + n
    }
}
// 执行结果
=== RUN   TestAddto
--- PASS: TestAddto (0.00s)
    bt_test.go:8: 结果正确
PASS

以上是关于golang 去文件的主要内容,如果未能解决你的问题,请参考以下文章

Golang读写文件的几种方式

golang 真棒golang去

golang单元测试

golang 去并发测试(我学习去)

一行一行读取文件没有换行符golang

golang实现chunk方式的查询