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 Memory 存储引擎的浅析本篇文章给大家分享的是有关如何进行MySQL Memory 存储引擎的浅析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小

    攻略 2021年12月20日
  • MVC3如何自定义注解验证字符长度

    技术MVC3如何自定义注解验证字符长度这篇文章主要介绍MVC3如何自定义注解验证字符长度,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!自定义注解(验证字符长度)需要继承ValidationAttr

    攻略 2021年11月23日
  • 右转的英文,直行然后右转英语怎么说

    技术右转的英文,直行然后右转英语怎么说直走然后向右转。 英语右转的英文:and then turn right.例句:1、Go straight ahead, then turn right.
    直走然后右转。
    2、A E

    生活 2021年10月20日
  • Java并发之同步器设计的方法是什么

    技术Java并发之同步器设计的方法是什么本篇内容介绍了“Java并发之同步器设计的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅

    攻略 2021年11月1日
  • MySQL项目中常用的19条优化方法分别是什么

    技术MySQL项目中常用的19条优化方法分别是什么MySQL项目中常用的19条优化方法分别是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获

    攻略 2021年11月29日
  • 汇编语言怎么实现各种码制的转换

    技术汇编语言怎么实现各种码制的转换本篇内容主要讲解“汇编语言怎么实现各种码制的转换”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“汇编语言怎么实现各种码制的转换”吧!1.十六进

    攻略 2021年11月29日