[Original] go Language Learning (three) string string, time and date type date

table of Contents

1. terminated string parsing processing principles
2. Time Type
3 Type Date

Principle analytical processing terminated string

1. underlayer is terminated string ⼀ a byte array, and can be [] interchangeable type byte
character string among the strings are not 2 can not be changed, how to modify it
3. byte by byte string string composition, so ⻓ length of the string is the string length byte byte ⻓
4.rune utf8 character used to represent the type of Use, rune ⼀ a character byte by one or more of the composition

Exercise
Exercise 1: Write a program ⼀, the British files into text strings string ⾏ trekking in reverse order.

Exercise 2: ⼀ write a program, a string comprising the string in the text files into ⾏ trekking reverse.

// reverse character output: text olleh
testReverseStringV2 function () {
	// rune represents the UTF8 character
	var str = "hello Chinese"
	// English can be byte
	// was r [] = byte [] bytes (str)
	was r [] rune = [] rune (str)

	for i := 0; i < len(r)/2; i++ {
		tmp: = r [len (s) -in-1] - r [i]
		r[i] = tmp
	}
	str = string(r)
	fmt.Println(str)
}


Exercise 3: Write a program ⼀, ⼀ determine whether a string is a string of text files back.

// whether it is a palindrome
testHuiWen function () {
	// rune represents the UTF8 character
	var str = "hello Chinese"
	// English can be byte
	// was r [] = byte [] bytes (str)
	was r [] rune = [] rune (str)

	for i := 0; i < len(r)/2; i++ {
		tmp: = r [len (s) -in-1] - r [i]
		r[i] = tmp
	}
	str2 := string(r)
	if str2 == str {
		fmt.Println(str, "is huiwen")
	} else {
		fmt.Println(str, "is  not huiwen")
	}
}

  

Time and date type

1. time packet
2. time.Time type, used to represent the time Using
3 acquires the current time, now: Time.now = ()
.. 4. Time.now () Day (), Time.now () Minute ( ), Time.now () Month (), Time.now () Year ()..
5. The format, fmt.Printf ( "% 02d /% 02d% 02d% 02d:% 02d:% 02d", now. Year () ...)

6. Get the current timestamp, time.Now (). Unix () .
7. Time stamp transfer type.
Simple 8. Using the timer's use

9. time.Duration Use nanoseconds to represent
10. Some amount constants:

const (  
    Nanosecond  Duration = 1  
    Microsecond          = 1000 * Nanosecond  
    Millisecond          = 1000 * Microsecond  
    Second               = 1000 * Millisecond  
    Minute               = 60 * Second  
    Hour                 = 60 * Minute 
) 

  

8. Format:

now := time.Now() 
fmt.Println(now.Format(“02/1/2006 15:04”)) 
fmt.Println(now.Format(“2006/1/02 15:04”))
fmt.Println(now.Format(“2006/1/02”))

  

Exercise 1: Write a program ⼀, get the current time, and formatted into a form 2017/06/15 08:05:00

//练习1
func testFormat1() {
now := time.Now()
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
// 秒
send := now.Second()
//now.Format 模板格式化,指定2006年1月2日
// timeStr := now.Format("2006/01/02 15:04:05")
timeStr := fmt.Sprintf("%02d/%02d/%02d %02d:%02d:%02d\n", year, month, day, hour, minute, send)
fmt.Printf("time:%s\n", timeStr)
}

  

Exercise 2: Write ⼀ a program, and start trekking time-consuming statistical ⼀ piece of code, the unit accurate to microseconds.

// program execution time-consuming subtle
func testCost() {
	start := time.Now().UnixNano()
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond)
	}
	end := time.Now().UnixNano()
	cost := (end - start) / 1000
	// microseconds
	fmt.Printf("code cost:%d us", cost)
}

  

 

Guess you like

Origin www.cnblogs.com/wangshuyang/p/11694918.html