Go语言之内置函数与包函数

技术Go语言之内置函数与包函数 Go语言之内置函数与包函数一、内置函数
Golang中为了编程方便,提供了一些函数,这些函数可以直接使用,称之为Go的内置函数。详情查看:https://studygol

去语言之内置函数与包函数

一、内置函数

Golang中为了编程方便,提供了一些函数,这些函数可以直接使用,称之为去的内置函数。详情查看:https://学习golang。com/static/pkg doc/pkg/builtin。html文件的后缀

常数

弯曲件类型

类型字节

类型符文

(同Internationalorganizations)国际组织类型

int8型

int16型

int32型

int64型

类型uint

uint8型

uint16型

uint32型

uint64型

浮动32型

float64型

复杂64型

复杂128型

uintptr类型

类型字符串

类型错误

类型

类型一

类型集成类型

类型浮动类型

类型复杂类型

实函数(c复杂类型)浮点类型

func imag(c ComplexType)浮动类型

函数复数(r,I浮点类型)复数类型

新功能(类型)*类型

功能类型(类型、尺寸和类型)

函数帽型)整数

函数类型整数

函数追加(切片[]类型,元素.类型)[]类型

函数副本(dst,src[]类型)int

函数删除(映射[类型]类型1,键类型)

函数关闭(c)改变类型)

功能死机接口{})

func recover()接口{}

函数打印(参数.类型)

func println(args.类型)

如:len、new、make等内置函数。

1、len

用来求长度,比如字符串、数组等

包装主体

导入“fmt”

func main(){ 0

//len的使用

var字符串字符串='你好'

fmt .Println('字符串长度,len(str)) //str长度5

}

2、new

用来分配内存,主要用来分配值的类型,比如int、float系列等,返回的时指针。

包装主体

导入“fmt”

func main(){ 0

//新的使用

编号:=新(int) //创建一个数字变量,类型为*int,值为系统分配的地址

*数字=10

fmt .Printf('号的类型为%T,数字的值为%v ',数字,数字)//数字的类型为*整数,数字的值为0xc000014098

}

3、make

分配引用类型的内存。

二、包函数

(一)字符串常用函数

1、len

包装主体

导入“fmt”

func main(){ 0

var str string='hello北京'

fmt .Println(len(str))

}

2、[]rune()

循环遍历字符串,并且处理中文乱码问题。

包装主体

导入“fmt”

func main(){ 0

var str string='hello北京'

str 2 :=[]符文(字符串)

对于I :=0;I len(str 2);我

fmt .Printf('i=%c\n ',st

r2[i])
}

}
/*
i=h
i=e
i=l
i=l
i=o
i=北
i=京
*/

3、字符串转整数

package main
import (
    "fmt"
    "strconv"
 )
func main() {
    number, error := strconv.Atoi("123")
    if error != nil {
        fmt.Println("error=", error)
    } else {
        fmt.Println("number=", number) // number= 123
    }
    
}

4、整数转字符串

package main
import (
    "fmt"
    "strconv"
 )
func main() {
    str := strconv.Itoa(123)
    fmt.Printf("str值为%v,str类型为%T",str,str) // str值为123,str类型为string
    
}

5、字符串转 []byte

package main
import (
    "fmt"
 )
func main() {
    var bytes = []byte("hello")
    fmt.Printf("bytes=%v", bytes) // bytes=[104 101 108 108 111]
    
}

6、[]byte转字符串

package main
import (
    "fmt"
 )
func main() {
    str := string([]byte{65, 66, 67})
    fmt.Printf("str=%v", str) // str=ABC
    
}

7、十进制转二、八、十六进制

package main
import (
    "fmt"
    "strconv"
 )
func main() {
    str1 := strconv.FormatInt(123, 2)
    fmt.Printf("123对应的二进制为%v\n",str1) // str值为123,str类型为string
    str2 := strconv.FormatInt(123, 16)
    fmt.Printf("123对应的十六进制为%v",str2) // str值为123,str类型为string
    
}
/*
123对应的二进制为1111011
123对应的十六进制为7b
*/

8、查找子串是否在指定的字符串

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.Contains("abcdef", "abc")
    fmt.Printf("str=%v", str) // str=true
    
}

9、统计一个字符串有几个指定的子串

package main
import (
    "fmt"
    "strings"
 )
func main() {
    num := strings.Count("abcdef", "abc")
    fmt.Printf("num=%v", num) // num=1
    
}

10、不区分大小写的字符串比较

package main
import (
    "fmt"
    "strings"
 )
func main() {
    ifFlag := strings.EqualFold("ABC", "abc")
    fmt.Printf("ifFlag=%v", ifFlag) // ifFlag=true
    
}

11、返回子串在字符串第一次出现的index值,如果没有返回-1

package main
import (
    "fmt"
    "strings"
 )
func main() {
    position := strings.Index("ABA", "A")
    fmt.Printf("position=%v", position) // position=0
    
}

12、返回子串在字符串最后一个出现的index,如果没有就返回-1

package main
import (
    "fmt"
    "strings"
 )
func main() {
    index := strings.LastIndex("ABC", "C")
    fmt.Printf("str index=%v", index) // str index=2
    
}

13、子串替换

将指定的子串替换成另一个子串,strings.Replace(str,"old","new1","new2",n),n指定替换的个数,如果n=-1表示全部替换。

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.Replace("ABA", "Z", "Z", -1)
    fmt.Printf("str=%v", str) // str=ABA
    
}

14、分割字符串

按照指定的某个字符为分隔符,将一个字符串拆成字符串数组。

package main
import (
    "fmt"
    "strings"
 )
func main() {
    strArr := strings.Split("A,B,C,D", ",")
    fmt.Printf("strArr=%v", strArr) // strArr=[A B C D]
    
}

15、字符串字母进行大小写转换

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := "Hello World"
    str1 := strings.ToLower(str)
    str2 := strings.ToUpper(str)
    fmt.Printf("str1=%v\n", str1)
    fmt.Printf("str2=%v", str2)
    
}
/*
str1=hello world
str2=HELLO WORLD
*/

16、将字符串左右两边的空格去掉

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.TrimSpace(" Hello World ")
    fmt.Printf("str=%v", str) // str=Hello World
    
}
/*
str1=hello world
str2=HELLO WORLD
*/

17、去掉字符串左右两边指定的字符

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.Trim(" hello world ", "")
    fmt.Printf("str=%q\n", str) // str=" hello world "
    
}

18、去掉字符串左边指定的字符

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.TrimLeft(" hello world", "")
    fmt.Printf("str=%q\n", str) // str=" hello world"
    
}

19、去掉字符串右边指定的字符

package main
import (
    "fmt"
    "strings"
 )
func main() {
    str := strings.TrimRight(" hello world", "")
    fmt.Printf("str=%q\n", str) // str=" hello world"
    
}

20、判断字符串是否以指定字符串开头

package main
import (
    "fmt"
    "strings"
 )
func main() {
    IsPrefix := strings.HasPrefix("hello world", "hello")
    fmt.Printf("IsPrefix=%v\n", IsPrefix) // IsPrefix=true
    
}

21、判断字符串是否以指定字符串结尾

package main
import (
    "fmt"
    "strings"
 )
func main() {
    IsSuffix := strings.HasSuffix("hello world", "world")
    fmt.Printf("IsSuffix=%v\n", IsSuffix) // IsSuffix=true
    
}

(二)时间与日期相关函数

时间与日期相关函数,需要导入time包。

详情查看https://studygolang.com/static/pkgdoc/pkg/time.htm:

  • func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
  • func Parse(layout, value string) (Time, error)
  • func ParseInLocation(layout, value string, loc *Location) (Time, error)
  • func Now() Time
  • func Unix(sec int64, nsec int64) Time
  • func (t Time) Location() *Location
  • func (t Time) Zone() (name string, offset int)
  • func (t Time) IsZero() bool
  • func (t Time) Local() Time
  • func (t Time) UTC() Time
  • func (t Time) In(loc *Location) Time
  • func (t Time) Unix() int64
  • func (t Time) UnixNano() int64
  • func (t Time) Equal(u Time) bool
  • func (t Time) Before(u Time) bool
  • func (t Time) After(u Time) bool
  • func (t Time) Date() (year int, month Month, day int)
  • func (t Time) Clock() (hour, min, sec int)
  • func (t Time) Year() int
  • func (t Time) Month() Month
  • func (t Time) ISOWeek() (year, week int)
  • func (t Time) YearDay() int
  • func (t Time) Day() int
  • func (t Time) Weekday() Weekday
  • func (t Time) Hour() int
  • func (t Time) Minute() int
  • func (t Time) Second() int
  • func (t Time) Nanosecond() int
  • func (t Time) Add(d Duration) Time
  • func (t Time) AddDate(years int, months int, days int) Time
  • func (t Time) Sub(u Time) Duration
  • func (t Time) Round(d Duration) Time
  • func (t Time) Truncate(d Duration) Time
  • func (t Time) Format(layout string) string
  • func (t Time) String() string
  • func (t Time) GobEncode() ([]byte, error)
  • func (t *Time) GobDecode(data []byte) error
  • func (t Time) MarshalBinary() ([]byte, error)
  • func (t *Time) UnmarshalBinary(data []byte) error
  • func (t Time) MarshalJSON() ([]byte, error)
  • func (t *Time) UnmarshalJSON(data []byte) error
  • func (t Time) MarshalText() ([]byte, error)
  • func (t *Time) UnmarshalText(data []byte) error

1、time.Time

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    fmt.Printf("now=%v type=%T", now, now) // now=2021-11-21 21:55:00.8398932 +0800 CST m=+0.004958701 type=time.Time
}

2、获取日期其它参数

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    // 通过now获取年月日、时分秒
    fmt.Printf("年=%v\n", now.Year())
    fmt.Printf("月=%v\n", now.Month())
    fmt.Printf("日=%v\n", now.Day())
    fmt.Printf("时=%v\n", now.Hour())
    fmt.Printf("分=%v\n", now.Minute())
    fmt.Printf("秒=%v\n", now.Second())
}
/*
年=2021
月=November
日=21      
时=21
分=58
秒=45
*/

3、格式化时间

  • Printf
  • Sprintf
  • time.Format

Printf:

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    // 通过now获取年月日、时分秒
    fmt.Printf("年月日%d-%d-%d %d:%d:%d", now.Year(),
    now.Month(),now.Day(),now.Hour(),
    now.Minute(),now.Second()) // 年月日2021-11-21 22:4:53
}

Sprintf:

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    // 通过now获取年月日、时分秒
    date := fmt.Sprintf("年月日%d-%d-%d %d:%d:%d", now.Year(),
    now.Month(),now.Day(),now.Hour(),
    now.Minute(),now.Second()) 
    fmt.Printf("date=%v", date) // date=年月日2021-11-21 22:7:43
}

time.Format:

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    // 通过now获取年月日、时分秒
    fmt.Printf(now.Format("2006-01-02 15:04:05")) // 2021-11-21 22:10:11
    fmt.Printf(now.Format("2006-01-02")) // 2021-11-21
    fmt.Printf(now.Format("15:04:05")) // 22:10:11
}

其中“2006/01/02 15:04:05” 这个字符串的数字时固定的,必须这样写。但是字符串的各个数字可以相互组合,因为数字都是唯一的。

4、时间常量

// Duration类型代表两个时间点之间经过的时间,以纳秒为单位。可表示的最长时间段大约290年。
const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

作者:iveBoy
出处:http://www.cnblogs.com/shenjianping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/112082.html

(0)

相关推荐

  • MySQL常用分库分表方案有哪些

    技术MySQL常用分库分表方案有哪些这篇文章主要为大家展示了“MySQL常用分库分表方案有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“MySQL常用分库分表方案有哪些”

    攻略 2021年11月14日
  • 用好像写一句话,用好像……好像……好像造句

    技术用好像写一句话,用好像……好像……好像造句1、天空中的云变幻无穷,有的像小狗,好像在伏在地上打盹用好像写一句话;有的像野狼,好像在仰天长啸;有的像雄鹰,好像在展翅飞翔。2、花园里的花真是千姿百态:有的傲立在枝头,好像

    生活 2021年10月22日
  • SpringBoot整合Redisson如何实现分布式锁

    技术SpringBoot整合Redisson如何实现分布式锁这篇文章将为大家详细讲解有关SpringBoot整合Redisson如何实现分布式锁,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所

    攻略 2021年11月13日
  • 小白点,人的面部出现小白点是怎么回事

    技术小白点,人的面部出现小白点是怎么回事先说说脂肪粒的产生 1、 体内原因:眼部、面部出现油脂粒大多是由于近期身体内分泌有些失调小白点,致使面部油脂分泌过剩,再加上皮肤没有得到彻底清洁干净,导致毛孔阻塞,很快形成脂肪粒。

    生活 2021年10月25日
  • 百万级MySQL的数据量怎么快速完成数据迁移

    技术百万级MySQL的数据量怎么快速完成数据迁移这篇文章主要讲解了“百万级MySQL的数据量怎么快速完成数据迁移”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“百万级MyS

    攻略 2021年11月18日
  • css如何清除背景图片重复

    技术css如何清除背景图片重复这篇文章主要为大家展示了“css如何清除背景图片重复”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css如何清除背景图片重复”这篇文章吧。

    攻略 2021年11月14日