Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b586b3

Browse files
committed
Golang中文社区 - Go语言学习园地
1 parent 98a38fd commit 1b586b3

File tree

102 files changed

+7003
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+7003
-0
lines changed

websites/code/studygolang/install.bat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@echo off
2+
3+
setlocal
4+
5+
if exist install.bat goto ok
6+
echo install.bat must be run from its folder
7+
goto end
8+
9+
:ok
10+
11+
set OLDGOPATH=%GOPATH%
12+
set GOPATH=%~dp0;%~dp0..\thirdparty
13+
14+
go install studygolang
15+
go install test
16+
17+
set GOPATH=%OLDGOPATH%
18+
19+
:end
20+
echo finished
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package config
2+
3+
import (
4+
"process"
5+
"path"
6+
)
7+
8+
// 项目根目录
9+
var ROOT string
10+
11+
func init() {
12+
binDir, err := process.ExecutableDir()
13+
if err != nil {
14+
panic(err)
15+
}
16+
ROOT = path.Dir(binDir)
17+
}
18+
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package admin
2+
3+
import (
4+
"config"
5+
"html/template"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
var ROOT = config.ROOT
11+
12+
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
13+
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/index.html")
14+
if err != nil {
15+
fmt.Fprintln(rw, err)
16+
return
17+
}
18+
tpl.Execute(rw, nil)
19+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package admin
2+
3+
import (
4+
"html/template"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
func UsersHandler(rw http.ResponseWriter, req *http.Request) {
11+
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/users.html")
12+
if err != nil {
13+
fmt.Fprintln(rw, err)
14+
return
15+
}
16+
tpl.Execute(rw, nil)
17+
}
18+
19+
// 添加新用户表单页面
20+
func NewUserHandler(rw http.ResponseWriter, req *http.Request) {
21+
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/newuser.html")
22+
if err != nil {
23+
fmt.Fprintln(rw, err)
24+
return
25+
}
26+
tpl.Execute(rw, nil)
27+
}
28+
29+
func Validate(data url.Values, rules map[string]map[string]string) (errMsg string) {
30+
for field, rule := range rules {
31+
val := data.Get(field)
32+
if _, ok := rule["require"]; ok {
33+
if val == "" {
34+
errMsg = field + "不能为空"
35+
return
36+
}
37+
}
38+
39+
if valRange, ok := rule["range"]; ok {
40+
41+
}
42+
}
43+
}
44+
45+
// 执行添加新用户(异步请求,返回json)
46+
func AddUserHandler(rw http.ResponseWriter, req *http.Request) {
47+
errMsg := ""
48+
49+
username := req.FormValue("username")
50+
email := req.FormValue("email")
51+
// name := req.FormValue("name")
52+
passwd := req.FormValue("passwd")
53+
pass2 := req.FormValue("pass2")
54+
// send_password := req.FormValue("send_password")
55+
56+
if username == "" {
57+
errMsg = "用户名不能空!"
58+
}
59+
60+
if email == "" {
61+
errMsg = "邮箱不能空!"
62+
}
63+
64+
if passwd == "" {
65+
errMsg = "密码不能空!"
66+
}
67+
68+
if passwd != pass2 {
69+
errMsg = "两次密码不一致!"
70+
}
71+
// TODO: 其他判断
72+
header := rw.Header()
73+
header.Set("Content-Type", "application/json; charset=utf-8")
74+
if errMsg != "" {
75+
fmt.Fprint(rw, `{"error":"`, errMsg, `"}`)
76+
return
77+
}
78+
79+
// 入库
80+
}
81+
82+
func ProfilerHandler(rw http.ResponseWriter, req *http.Request) {
83+
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/profiler.html")
84+
if err != nil {
85+
fmt.Fprintln(rw, err)
86+
return
87+
}
88+
tpl.Execute(rw, nil)
89+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package controller
2+
3+
import (
4+
"config"
5+
"html/template"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
var ROOT = config.ROOT
11+
12+
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
13+
tpl, err := template.ParseFiles(ROOT + "/template/index.html", ROOT + "/template/common/header.html", ROOT + "/template/common/footer.html")
14+
if err != nil {
15+
fmt.Fprintln(rw, err)
16+
return
17+
}
18+
tpl.Execute(rw, nil)
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package controller
2+
3+
import (
4+
"html/template"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
10+
tpl, err := template.ParseFiles(ROOT + "/template/topics/new.html", ROOT + "/template/common/header.html", ROOT + "/template/common/footer.html")
11+
if err != nil {
12+
fmt.Fprintln(rw, err)
13+
return
14+
}
15+
tpl.Execute(rw, nil)
16+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package model
2+
3+
import (
4+
"database/sql"
5+
_ "github.com/Go-SQL-Driver/MySQL"
6+
"strings"
7+
"util"
8+
)
9+
10+
type Dao struct {
11+
*sql.DB
12+
// 构造sql语句相关
13+
tablename string
14+
where string
15+
whereVal []interface{} // where条件对应中字段对应的值
16+
limit string
17+
order string
18+
// 插入需要
19+
columns []string // 需要插入数据的字段
20+
colValues []interface{} // 需要插入字段对应的值
21+
// 查询需要
22+
selectCols string // 想要查询那些字段,接在SELECT之后的,默认为"*"
23+
}
24+
25+
func (this *Dao) Open() (err error) {
26+
this.DB, err = sql.Open("mysql", "root:@/studygolang?charset=utf8")
27+
return
28+
}
29+
30+
// Insert 插入数据
31+
func (this *Dao) Insert() (sql.Result, error) {
32+
strSql := util.InsertSql(this)
33+
err := this.Open()
34+
if err != nil {
35+
return nil, err
36+
}
37+
defer this.Close()
38+
stmt, err := this.Prepare(strSql)
39+
if err != nil {
40+
return nil, err
41+
}
42+
defer stmt.Close()
43+
return stmt.Exec(this.ColValues()...)
44+
}
45+
46+
// Find 查找单条数据
47+
func (this *Dao) Find() (*sql.Row, error) {
48+
strSql := util.SelectSql(this)
49+
err := this.Open()
50+
if err != nil {
51+
return nil, err
52+
}
53+
defer this.Close()
54+
stmt, err := this.Prepare(strSql)
55+
if err != nil {
56+
return nil, err
57+
}
58+
defer stmt.Close()
59+
return stmt.QueryRow(this.whereVal...), nil
60+
}
61+
62+
func (this *Dao) Columns() []string {
63+
return this.columns
64+
}
65+
66+
func (this *Dao) ColValues() []interface{} {
67+
return this.colValues
68+
}
69+
70+
func (this *Dao) SetWhere(args ...string) {
71+
fields := make([]string, len(args))
72+
this.whereVal = make([]interface{}, len(args))
73+
for i, arg := range args {
74+
parts := strings.SplitN(arg, "=", 2)
75+
if len(parts) != 2 {
76+
// TODO:怎么处理
77+
}
78+
fields[i] = parts[0] + "=?"
79+
this.whereVal[i] = parts[1]
80+
}
81+
this.where = strings.Join(fields, " AND ")
82+
}
83+
84+
func (this *Dao) SelectCols() string {
85+
if this.selectCols == "" {
86+
return "*"
87+
}
88+
return this.selectCols
89+
}
90+
91+
func (this *Dao) Where() string {
92+
return this.where
93+
}
94+
95+
func (this *Dao) SetOrder(order string) {
96+
this.order = order
97+
}
98+
99+
func (this *Dao) Order() string {
100+
return this.order
101+
}
102+
103+
func (this *Dao) SetLimit(limit string) {
104+
this.limit = limit
105+
}
106+
107+
func (this *Dao) Limit() string {
108+
return this.limit
109+
}
110+
111+
func (this *Dao) Tablename() string {
112+
return this.tablename
113+
}
114+
115+
type ORMer interface {
116+
Insert()
117+
Update()
118+
Find()
119+
FindAll()
120+
Delete()
121+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package model
2+
3+
import (
4+
"time"
5+
)
6+
7+
const (
8+
tablename = "user_login"
9+
)
10+
11+
type UserLogin struct {
12+
Uid int `json:"uid"`
13+
Username string `json:"username"`
14+
Passwd string `json:"passwd"`
15+
Email string `json:"email"`
16+
passcode string // 加密随机串
17+
18+
// 数据库访问对象
19+
*Dao
20+
}
21+
22+
func NewUserLogin() *UserLogin {
23+
return &UserLogin{
24+
Dao: &Dao{tablename: tablename},
25+
}
26+
}
27+
28+
func (this *UserLogin) Insert() (int64, error) {
29+
this.prepareInsertData()
30+
result, err := this.Dao.Insert()
31+
if err != nil {
32+
return 0, err
33+
}
34+
return result.RowsAffected()
35+
}
36+
37+
func (this *UserLogin) Find() error {
38+
row, err := this.Dao.Find()
39+
if err != nil {
40+
return err
41+
}
42+
return row.Scan(&this.Uid, &this.Username, &this.Passwd, &this.Email, &this.passcode)
43+
}
44+
45+
func (this *UserLogin) prepareInsertData() {
46+
this.columns = []string{"uid", "username", "passwd", "email", "passcode"}
47+
this.passcode = "123ff"
48+
this.colValues = []interface{}{this.Uid, this.Username, this.Passwd, this.Email, this.passcode}
49+
}
50+
51+
type User struct {
52+
Uid int
53+
Username string
54+
Passwd string
55+
Email string
56+
Name string
57+
Avatar string
58+
City string
59+
Company string
60+
Github string
61+
Weibo string
62+
Website string
63+
Status string
64+
Introduce string
65+
// 不导出
66+
open int
67+
ctime time.Time
68+
}
69+
70+
func NewUser() *User {
71+
return nil
72+
}

0 commit comments

Comments
 (0)