当前位置:网站首页>Go's relevant operation and arrangement of documents (continuous update)
Go's relevant operation and arrangement of documents (continuous update)
2022-04-21 14:07:00 【Roc. Chang】
Catalog
First contact Go Development of , Roll up !!!
1. Get current go File directory
Just started recently GORM (go Of ORM frame ), It is found that... Is not automatically generated according to database fields Object( stay Go The corresponding in is struct ) Tool class of , So I rolled one of my own hands , It can be used , In the process of writing, I happen to encounter the problem of obtaining the current go The path of the file , So I checked the documents , I was surprised when I found it , little does one think GO It's such a grammar , although Java This function is also available in , But it doesn't feel coquettish enough !! Ha ha ha !
// Source location : os/getwd.go
// Get current go The directory of the file
path, err := os.Getwd()
if err != nil {
log.Println(err)
} else {
fmt.Println(path)
}
2. Get the file path separator of the system
Because the system is different , Some people are Windows( The separator is //), Some people are Linux( The separator is \), Others are Mac( The separator is \). If you want to write cross platform code generation tools that are compatible , You need compatible path separators instead of hard coding them in . If this is true , Go It has also been taken into account , And the calling methods of different systems are the same .
// Windwos Separator source code location : os/path_windows.go
// Linux and Mac Separator source code location : os/path_unix.go
// Usage mode (Go Will judge according to the actual system )
separator := string(os.PathSeparator)
Here's the problem , When you look in the code, you will find PathSeparator It's not a string , It's a character . stay Go in , The character type is rune type ,rune yes int32 The nickname , So we need to use string() Function to a string , And then you can use it .
package os
const (
PathSeparator = '/' // OS-specific path separator
PathListSeparator = ':' // OS-specific path list separator
)
package os
const (
PathSeparator = '\\' // OS-specific path separator
PathListSeparator = ';' // OS-specific path list separator
)
3. Go Determine if a file or directory exists
It's a pit here , The main nausea is Go Of os There is one in the bag IsNotExist function , One more IsExist function ; But actually IsExist And IsNotExist It's not an opposite relationship , That is to use IsNotExist by false The place of , Use IsExist for true, It feels like a pit . If you want to correctly judge whether a file or directory exists , Remember not to use IsExist, But use IsNotExist .
The right way to use :
// UNIX Design principles : Everything is a document , In other words, directories and files are files . Can be judged in the following way
// file, err := os.Stat("/Users/roc/Downloads/")
file, err := os.Stat("/Users/roc/Downloads/download.jpg")
if err != nil {
if os.IsNotExist(err) {
// It cannot be replaced by os.IsExist(err)
log.Fatalln(" The file does not exist ")
}
} else {
log.Println(" The file exists ",file.Name())
}
Counter example ( The wrong way to use ):
file, err := os.Stat("/Users/roc/Downloads/download.jpg")
if os.IsExist(err) {
// It cannot be replaced by os.IsExist(err)
log.Fatalln(" The file does not exist ")
}
// When err == nil It means that the file already exists , Whether the above file exists or not will not be judged
A separate article will be written later to distinguish IsExist And IsNotExist .
Continuous updating ing !!!
Personal blog : Roc’s Blog
版权声明
本文为[Roc. Chang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211351519413.html
边栏推荐
- centos 卸载mysql
- Differential mean value theorem
- Zabbix5系列-监控华为、H3C交换机(snmpv2c/snmpv3/snmptrap) (二)
- Web Course Design - photo recording website (flask)
- 大学英语词汇解析 中国大学mooc 华中科技大学 测验题答案
- How does redis query a key from massive data?
- web课程设计-照片记录网站(Flask)
- Zabbix5 series - sound alarm, mail alarm (XIV)
- Network port number and protocol number (Daquan)
- iscsi
猜你喜欢
随机推荐
Vous permet de jouer facilement avec le langage C scanf et getchar
报错:ModuleNotFoundError: No module named ‘astra‘
CEPH maintenance command understanding
极限存在准则 两个重要极限
Crawler example: climb the ranking of Chinese Universities
汇编语言程序设计 中国大学Mooc郑州大学 网课 测试题目和答案
做题笔记(一)
Infinitesimal and infinity
原子类的使用与原理
应用打包还是测试团队老大难问题?
comparison of infinitesimal
技术分享 | Selenium 测试用例编写
基本功:SQL 多表联合查询的几种方式
Binary search of ordered array with C language
MySQL data backup management
MysQL读写分离服务器--maxscale服务
使用js获取网页数据,并进行格式化输出(网页爬取)
MYSQL配置PXC高可用集群
H5性能分析实战来啦~
Network port number and protocol number (Daquan)









