Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
31b672c5
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
31b672c5
编写于
10月 13, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
0f757a94
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
365 addition
and
4 deletion
+365
-4
blog/golang/golang-mongo.md
blog/golang/golang-mongo.md
+177
-0
blog/golang/golang-mysql.md
blog/golang/golang-mysql.md
+154
-0
blog/golang/index.md
blog/golang/index.md
+8
-4
weekly/20221017.md
weekly/20221017.md
+21
-0
weekly/index.md
weekly/index.md
+5
-0
未找到文件。
blog/golang/golang-mongo.md
0 → 100644
浏览文件 @
31b672c5
# Golang操作MongoDB数据库
## 下载安装MongoDB
https://www.mongodb.com/try/download/community2
https://www.mongodb.com/download-center/community/releases
连接客户端
```
bash
# 打开客户端
mongo
# 创建数据库
use go_db
;
# 创建集合
db.createCollection
(
'student'
)
;
```
## 下载驱动
https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo
```
go get go.mongodb.org/mongo-driver/mongo
```
## 连接到mongodb数据库
```
go
package
main
import
(
"context"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func
main
()
{
// 设置客户端连接配置
// db_url := "mongodb://root:123456@localhost:27017"
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
// 连接到mongo
client
,
_
:=
mongo
.
Connect
(
context
.
TODO
(),
clientOptions
)
// 连接检查
err
:=
client
.
Ping
(
context
.
TODO
(),
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
```
## BSON
二进制编码的json
类型D家族
-
D BSON文档
-
M 无序map
-
A BSON数组
-
E D里面的一个元素
示例
```
go
package
main
import
(
"fmt"
"go.mongodb.org/mongo-driver/bson"
)
func
main
()
{
d
:=
bson
.
D
{{
"name"
,
"age"
}}
fmt
.
Printf
(
"d: %v
\n
"
,
d
)
// d: [{name age}]
}
```
## 添加文档
插入单条记录
```
go
package
main
import
(
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type
Student
struct
{
Name
string
Age
int
}
func
main
()
{
// 连接到mongo
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
client
,
_
:=
mongo
.
Connect
(
context
.
TODO
(),
clientOptions
)
student
:=
Student
{
Name
:
"Tom"
,
Age
:
23
,
}
collection
:=
client
.
Database
(
"go_db"
)
.
Collection
(
"student"
)
ior
,
_
:=
collection
.
InsertOne
(
context
.
TODO
(),
student
)
fmt
.
Printf
(
"ior.InsertedID: %v
\n
"
,
ior
.
InsertedID
)
// ior.InsertedID: ObjectID("634822c35881b85ab2aa138e")
}
```
插入多条记录
```
go
package
main
import
(
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type
Student
struct
{
Name
string
Age
int
}
func
main
()
{
// 连接到mongo
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
client
,
_
:=
mongo
.
Connect
(
context
.
TODO
(),
clientOptions
)
student1
:=
Student
{
Name
:
"Steve"
,
Age
:
24
,
}
student2
:=
Student
{
Name
:
"Jack"
,
Age
:
25
,
}
students
:=
[]
interface
{}{
student1
,
student2
}
collection
:=
client
.
Database
(
"go_db"
)
.
Collection
(
"student"
)
imr
,
_
:=
collection
.
InsertMany
(
context
.
TODO
(),
students
)
fmt
.
Printf
(
"imr.InsertedIDs: %v
\n
"
,
imr
.
InsertedIDs
)
// imr.InsertedIDs: [
// ObjectID("634823e52758c71e01e1e78f")
// ObjectID("634823e52758c71e01e1e790")
// ]
}
```
\ No newline at end of file
blog/golang/golang-mysql.md
浏览文件 @
31b672c5
...
@@ -138,3 +138,157 @@ func main() {
...
@@ -138,3 +138,157 @@ func main() {
```
```
## 查询操作
单行查询
```
go
package
main
import
(
"database/sql"
"fmt"
_
"github.com/go-sql-driver/mysql"
)
type
User
struct
{
id
int
username
string
password
string
}
func
main
()
{
db_url
:=
"root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true"
db
,
_
:=
sql
.
Open
(
"mysql"
,
db_url
)
defer
db
.
Close
()
// 查询单条数据
var
user
User
sql
:=
"select * from user_tbl where id = ?"
db
.
QueryRow
(
sql
,
1
)
.
Scan
(
&
user
.
id
,
&
user
.
username
,
&
user
.
password
)
fmt
.
Printf
(
"row: %v
\n
"
,
user
)
// row: {1 Tom 123456}
}
```
查询多条数据
```
go
package
main
import
(
"database/sql"
"fmt"
_
"github.com/go-sql-driver/mysql"
)
type
User
struct
{
id
int
username
string
password
string
}
func
main
()
{
db_url
:=
"root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true"
db
,
_
:=
sql
.
Open
(
"mysql"
,
db_url
)
defer
db
.
Close
()
sql
:=
"select * from user_tbl"
rows
,
_
:=
db
.
Query
(
sql
)
defer
rows
.
Close
()
for
rows
.
Next
()
{
var
user
User
rows
.
Scan
(
&
user
.
id
,
&
user
.
username
,
&
user
.
password
)
fmt
.
Printf
(
"row: %v
\n
"
,
user
)
// row: {1 Tom 123456}
// row: {2 Kite abcdef}
// row: {3 Jack jjyy}
}
}
```
## 更新操作
```
go
package
main
import
(
"database/sql"
"fmt"
_
"github.com/go-sql-driver/mysql"
)
type
User
struct
{
id
int
username
string
password
string
}
func
main
()
{
db_url
:=
"root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true"
db
,
_
:=
sql
.
Open
(
"mysql"
,
db_url
)
defer
db
.
Close
()
// 更新数据
sql
:=
"update user_tbl set username = ? where id = ?"
result
,
_
:=
db
.
Exec
(
sql
,
"Tom-1"
,
1
)
// 影响行数
i
,
_
:=
result
.
RowsAffected
()
fmt
.
Printf
(
"i: %v
\n
"
,
i
)
// i: 1
}
```
## 删除数据
```
go
package
main
import
(
"database/sql"
"fmt"
_
"github.com/go-sql-driver/mysql"
)
type
User
struct
{
id
int
username
string
password
string
}
func
main
()
{
db_url
:=
"root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true"
db
,
_
:=
sql
.
Open
(
"mysql"
,
db_url
)
defer
db
.
Close
()
// 删除数据
sql
:=
"delete from user_tbl where id = ?"
result
,
_
:=
db
.
Exec
(
sql
,
3
)
// 影响行数
i
,
_
:=
result
.
RowsAffected
()
fmt
.
Printf
(
"i: %v
\n
"
,
i
)
// i: 1
}
```
blog/golang/index.md
浏览文件 @
31b672c5
...
@@ -100,13 +100,17 @@
...
@@ -100,13 +100,17 @@
-
[
5.13、Golang标准库-math
](
/blog/golang/golang-standard-math.md
)
-
[
5.13、Golang标准库-math
](
/blog/golang/golang-standard-math.md
)
[
Golang操作MySQL数据库
](
/blog/golang/golang-mysql.md
)
第六章 MySQL数据库
### 第六章 Golang操作数据库
-
[
Golang操作MySQL数据库
](
/blog/golang/golang-mysql.md
)
-
[
Golang操作MongoDB数据库
](
/blog/golang/golang-mongo.md
)
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=103&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=109&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
\ No newline at end of file
weekly/20221017.md
0 → 100644
浏览文件 @
31b672c5
# 全栈爱好者周刊|20221017
## 前端
30 个 Javascript 知识点总结,总有你不会的!
-
https://mp.weixin.qq.com/s/_UTPeZiLQIeFpTycMQXz2g
-
https://juejin.cn/post/7145036326373425159
现代 CSS 颜色指南
-
https://mp.weixin.qq.com/s/ZnfbAvjRGUz0f-oUomKd1g
教科书级图解 CSS Grid 布局,收藏了当字典用
-
https://mp.weixin.qq.com/s/WNvT3TO6HmlNSEorHwuB4Q
## 后端
万字长文:带你走进shell世界
-
https://mp.weixin.qq.com/s/gtIWO3ItxyLdQeVorXtShQ
weekly/index.md
0 → 100644
浏览文件 @
31b672c5
# 全栈爱好者周刊
[
全栈爱好者周刊|20221017
](
/weekly/20221017.md
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录