2021年9月7日 值对象与指针对象 假设有一个 map 对象 map[string]Person , 其中 Person 定义如下。 是一个 struct 1 2 3 type Person struct { Age int } 现在有一个需求, map 中的 Person 对象年龄为 0 , 则将其默认值设置为 18。 很显然, 由于 map[string]Person 中保存的是 值对象 ,因此通过任意方式获取的都是 值对象的副本 , 所有修改都是在副本上, 不能 修改真实值。 如果是 map[string]*Person 就很方便了。 *Person 是 指针……
阅读全文
2021年9月6日 GitlabCI 使用多个 Runner 执行特定 JOB 在 Gitlab CI 中,Runner 是 Job 的执行器, 也就是说 Job 的运行环境, 就是 Runner 的环境。 那么, 怎么将同一个 gitlab ci 中的 Job 运行在不同的 Runner 上呢? 例如, 根据 操作系统 区分, job1 运行在 windows 上, job2 运行在 linux 上, 诸如此类。 使用 TAG 指定 runner 其实很简单, gitlab ci 中, 可以通过指定 tags 来设定运行条件, 满足了 tag 才能被……
阅读全文
2021年9月6日 一篇文章告诉你 golang 环境变量的所有基础操作 原文链接: https://tangx.in/posts/2021/09/06/golang-os-env-operation/ golang 中的环境变量操作都在 os 包下面, 只有很少的几个方法, 而且字面意思也很明确。 所有环境变量操作对象都是 字符串string , 因此对于 int, bool 类型需要自己实现转换。 golang 程序执行的时候, 是在 linux 系统中 fork 的一种子进程中 golang程序 在 复制了开……
阅读全文
2021年9月2日 typora , 可以说一款为 github pages 网站量身定制的软件 配置初始目录 在 配置中 选择 General , 选择默认打开的目录。 配置图片路径 众所周知, Github Pages(Jekyll) 中, 文章需要放到 _post 下, 而资源应该另外创建目录, 如 assert 等。 这就造成了普通 markdown 编辑器插入图片的不方便。 解决方法如下: 使用图床, 彻底外部独立, 不存在相对路径的问题 放在 assert 下面, 但本……
阅读全文
2021年9月1日 Vue3 / React 静态网站项目容器化 - 实战案例 在前端容器化的时候, 有一个绕不开的问题: 容器返回的后端地址应该怎么设置。 静态编译到所有文件中, 肯定是不可取的, 总不能后端变更一个访问域名,前端都要重新构建一次镜像吧? 由于 js (typescript 编译后 ) 实际是运行在 用户的浏览器上, 所以也不能像后端一样读取环境变量。 所……
阅读全文
2021年9月1日 typescript 中使用 @ 路径别名 使用路径别名 @/some/path/index.ts 可以很简单的表示一个文件的绝对路径(其实是相对于 @ 的相对路径) 安装 @types/node 1 yarn add @types/node 配置 tsconfig.json , 一下是基于 vite2 项目配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { "compilerOptions": { // ... , "types": [ "node" ], // https://github.com/vitejs/vite/issues/279 "paths": { "@/*": [ "./src/*", ] } }, // ... } 就可以在 ts 文件中使用 @ 别名引入了。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17……
阅读全文
2021年8月31日 vue3 使用 vite2 初始化项目 vue3 + vite2 + typescript 配置 使用 vite2 创建项目 1 2 3 4 5 6 # 交换式 yarn create vite # 非交互式 yarn create vite project-name --template vue-ts 创建项目之后, cd project-name 进入项目, 是用 yarn 安装依赖, 使用 yarn dev 运行程序。 安装 less 支持 less 是 css 的一个超集。 yarn add less 安装之后, 可以在 CompName.vue 中使用 less 语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // CompName.vue <template> <div class="div1"> <h3>div1</h3> <div class="div2"> <h3>div2</h3> </div> </div> </template>……
阅读全文
2021年8月30日 一道 golang 切片面试题 为什么 sl[:5] 会返回底层数组的数据呢? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package main import "fmt" func main() { sl := make([]int, 0, 10) appendFn := func(s []int) { // 值传递, s 并不是 sl。 // 但数组是引用类型, 所以可以修改底层数组 fmt.Println("s ptr(old):", s) // [] s = append(s, 10, 20, 30) fmt.Println("s ptr(new):", s) // [10,20,30] } fmt.Println(sl) // [] appendFn(sl) fmt.Println(sl) // [] // 这里有点坑, 并不是取的 sl ,而是底……
阅读全文
2021年8月26日 golang 下划线完成对象的接口类型检查 在 Gin 源码中 有一行代码如下 1 var _ IRouter = &RouterGroup{} 乍一看, 是一个 赋值 操作, 但是前面又使用了 空白描述符(下划线) 。 这是什么意思呢? 答案是: 接口类型检查 在 《Effective GO》 Interface Check 中的描述有相关描述。 全文如下。 One place this situation arises is when it is necessary to guarantee within the package implementing the type that it actually satisfies the interface. If a type-for……
阅读全文
2021年8月26日 typescript 中的 const assertions const assertions - TypeScript 3.4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // vue3 const dnsProviders = { "aliyun.com": "alidns", "tencent.com": "dnspod" } let data = reactive({ rootDomain: "aliyun.com" as const }) let dnsProvider = computed( () => { return dnsProviders[data.rootDomain] } ) 这个时候会, 提示 7053 错误, data.rootDomain 具有 any type, 不能被用作 key。 解决这个问题使用, 需要使用 typescript 中 const assertion 类型推断。 const assertion 类型推断。 字面量类型推断: 其类型为字面值类型。 例如这里的 hello 的类型是……
阅读全文