GORM 字段使用自定义类型


起步

想在使用 GORM 时使用自定义类型必然事出有因,一般可有以下两种方式:

  • 方法 1:
type MyString string
  • 方法 2:
type MyString struct {
    string
}

当需求比较简单时,可采取方法1,也就是类型别名;如果需求复杂,就不得不把数据字段嵌入自定义结构体中。字段是否匿名并不重要,主要是用来承载目的数据。


Go 方法接收器与接口


在写 Go 的时候,我们常会发现以下情况:

type Z struct {
}

func (zv Z) Hello() {
    log.Println("hello")
}

func (zp *Z) World() {
    log.Println("world")
}

func main() {
    {
        zv := Z{}
        zv.Hello()
        zv.World()
    } // 正常执行

    {
        zp := &Z{}
        zp.Hello()
        zp.World()
    } // 正常执行
}


fetch 引发 blocked by CORS policy


起步

当使用 fetch 函数做跨域请求时,大概率会在浏览器 Console 中看到这样一个错误信息:Access to fetch at 'xxx' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.