那么我们在外部包中,可以直接通过下面代码访问admin结构体内部的属性:
admin := pkgname.Admin() fmt.Println(admin.Name, admin.Email)
当然这种情况下,需要你事先知道admin的结构以及包含的属性名。
内置类型和自定义类型
Go语言包含了几种简单的内置类型:整数、布尔值、数组、字符串、分片、映射等。除了内置类型,Go语言还支持方便的自定义类型。
自定义类型有两种:
-
自定义结构体类型: type MyType struct {}这种形式定义,这种类似C语言中的结构体定义。
命名类型: type MyInt int。这种方式通过将内置类型或自定义类型命名为新的类型的方式来实现。 需要注意MyInt和int是不同的类型,它们之间不能直接互相赋值。
函数和方法
Go语言的函数和方法都是使用func关键词声明的,方法和函数的唯一区别在于,方法需要绑定目标类型; 而函数则无需绑定。
type MyType struct {
}
// 这是函数
func DoSomething() {
}
// 这是方法
func (mt MyType) MyMethod() {
}
// 或者另外一种类型的方法
func (mt *MyType) MyMethod2() {
}
对于方法来说,需要绑定一个receiver, 我称之为接收者。 接收者有两种类型:
-
值类型的接收者
指针类型的接收者
关于接收者和接口部分,有很多需要延伸的,后续有时间整理补充出来。
接口
代码分析
常量部分
代码分析部分,我们先跳过import部分, 直接进入到常量的声明部分。
const (
// ContentType represents content type
ContentType string = "Content-Type"
// ContentJSON represents content type application/json
ContentJSON string = "application/json"
// ContentJSONP represents content type application/javascript
ContentJSONP string = "application/javascript"
// ContentXML represents content type application/xml
ContentXML string = "application/xml"
// ContentYAML represents content type application/x-yaml
ContentYAML string = "application/x-yaml"
// ContentHTML represents content type text/html
ContentHTML string = "text/html"
// ContentText represents content type text/plain
ContentText string = "text/plain"
// ContentBinary represents content type application/octet-stream
ContentBinary string = "application/octet-stream"
// ContentDisposition describes contentDisposition
ContentDisposition string = "Content-Disposition"
// contentDispositionInline describes content disposition type
contentDispositionInline string = "inline"
// contentDispositionAttachment describes content disposition type
contentDispositionAttachment string = "attachment"
defaultCharSet string = "utf-8"
defaultJSONPrefix string = ""
defaultXMLPrefix string = `<?xml version="1.0" encoding="ISO-8859-1" ?>n`
defaultTemplateExt string = "tpl"
defaultLayoutExt string = "lout"
defaultTemplateLeftDelim string = "{{"
defaultTemplateRightDelim string = "}}"
)










