GO笔记

创建字符串

var str = "Hello world!"

str := "Hello world!"

 字符串长度

len(str) 返回包含在字符串文字中的字节数

 连接字符串

strings包包含一个用于连接多个字符串的join()方法

eg:

strings.Join(sample, " ")

Join连接数组的元素以创建单个字符串。第二个参数是分隔符,放置在数组的元素之间。

eg:

strings.Join(数组,分隔符)//类似PHP implode

defaultFormat := "2006-01-02 15:04:05 PM -07:00 Jan Mon MST"

 格式化时间

now := time.Now().Format("2006-01-02 15:04:05")

 //时间反格式化成时间戳

    the_time, err := time.ParseInLocation("2006-01-02", "2015-12-01", time.Local)

获取时间戳

time.Now().Unix()

//时间反格式化成时间戳(月份必须是month)

    the_time := time.Date(2016, 1, 5, 0, 0, 0, 0, time.Local)

    unix_time1 := the_time.Unix()

//使用time.Parse

    the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")

    if err == nil {

        unix_time := the_time.Unix()

fmt.Println(unix_time)

}

#string到int

int,err:=strconv.Atoi(string)

#string到int64

int64, err := strconv.ParseInt(string, 10, 64)

#int到string

string:=strconv.Itoa(int)

#int64到string

string:=strconv.FormatInt(int64,10)

#Go指针#

Go语言的取地址符 & 用于返回相应变量的内存地址 eg: &a

一个指针变量指向一个值的内存地址

在使用指针前需要声明指针 格式: var var_name *var-type

var-type 为指针类型, var_name 为指针变量名, * 号用于指定变量是作为一个指针。eg: var ip *int /*指向整形*/

在指针类型前面加上 * 号来获取指针所指向的内容 eg: *ip

#Go空指针#

当一个指针被定义后没有分配到任何变量时,他的值为 nil nil指针也成为空指针 if(ptr == nil)

#Go指针数组#

结构 var ptr [...]*int /*声明整形指针数组*/

#Go语言Map#

Map 是一种无序的键值对的集合

定义映射 Map

必须使用make函数来创建映射

/* 声明变量,默认 map 是 nil */

var map_variable map[key_data_type]value_data_type

/* 使用 make 函数 *

map_variable := make(map[key_data_type]value_data_type)

eg: 

countryCapitalMap := map[string]string{"France":"Paris","Italy":"Rome","Japan":"Tokyo","india":"NewDelhi"}

for country := range countryCapitalMap{}

for country,capital := range countryaCapitalMap{}

delete()函数

delete()函数用于从映射中删除指定的相应键

eg:

delete(countryCapitalMap,"France")

#GO接口#

语法

/* define an interface */

type interface_name interface {

   method_name1 [return_type]

   method_name2 [return_type]

   method_name3 [return_type]

   ...

   method_namen [return_type]

}

/* define a struct */

type struct_name struct {

   /* variables */

}

/* implement interface methods*/

func (struct_name_variable struct_name) method_name1() [return_type] {

   /* method implementation */

}

...

func (struct_name_variable struct_name) method_namen() [return_type] {

   /* method implementation */

}

#结构体#

结构体定义需要使用type和 struct语句

语法格式:

type struct_variable_type struct{

member definition; /*成员 定义*/

member definition;

...

member definition;

}

定义的结构体类型,能用于变量的声明 语法格式:

variable_name := structure_variable_type{value1,value2,...,valuen}

variable_name := structure_variable_type{key1:value1,key2:value2,...,keyn:valuen}

结构体成员

访问结构体成员使用 点号 . 操作符 格式: structure_variable_type.member

结构体指针

语法格式: var struct_pointer *structure_variable_type

查看结构体变量地址:struct_pointer = &structure_variable_type

使用结构体指针访问其成员:struct_pointer.member

type mystr string //自定义类型,给类型改名

type Stu struct{

name string

int

mystr

}

① s := Stu{"name",1,"abc"}

② new(Stu) //分配空间再赋值

你可能感兴趣的:(GO笔记)