In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail the sample analysis on the basis of Golang. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The first golang program, package mainimport "fmt" func main () {fmt.Println ("hello golang")} basic data type
Boolean (true or false)
Numeric types (integer int and floating point float32, float64)
String type (a string is a sequence of characters concatenated by a fixed length of characters)
Derived type:
Pointer type (Pointer)
Array type
Structured types (struct)
Channel Typ
Function type
Slice Typ
Interface type (interface)
Map Typ
Numeric type
Shaping
Uint8 unsigned 8-bit integers (0 to 255)
Uint16 unsigned 16-bit integer (0 to 65535)
Uint32 unsigned 32-bit integer (0 to 4294967295)
Uint64 unsigned 64-bit integer (0 to 18446744073709551615)
Int8 signed 8-bit integers (- 128to127)
Int16 signed 16-bit integers (- 32768 to 32767)
Int32 signed 32-bit integers (- 2147483648 to 2147483647)
Int64 signed 64-bit integers (- 9223372036854775808 to 9223372036854775807)
Floating point type
Float32 32-bit floating point number
Float64 64-bit floating point number
Complex64 32-bit real and imaginary numbers
Complex128 64-bit real and imaginary numbers
Other numeric types
Byte is similar to uint8
Rune is similar to int32
Uint 32 or 64 bit
Int is the same size as uint
Uintptr unsigned integer, used to store a pointer
Define variable / / declare a variable var identifier type// can declare more than one variable var identifier1 at a time, identifier2 type// determines the variable type according to the value var v_name = value// short form omits var Note: = if the new variable v_name: = value definition constant / / declare a constant const identifier [type] = value// explicit type definition const b string = "abc" / / implicit type definition const b = "abc" / / multiple declarations of the same type (implicit type definition) const c_name1, c_name2 = value1, value2iota
Iota, a special constant, can be thought of as a constant that can be modified by the compiler
Iota will be reset to 0 when the const keyword appears (before the first line inside the const), and each new row of constant declaration in const will make the iota count (iota can be understood as the row index in the const statement block).
Iota can be used as an enumerated value
Package mainimport "fmt" func main () {const (a = iota / / 0b / / 1 c / / 2 d = "ha" / / independent value Iota + = 1e / / "ha" iota + = 1f = 100g / / iota + = 1g / / 100iota + = 1h = iota / / 7, recovery count I / / 8) fmt.Println
Running result
0 1 2 ha ha 100 100 7 8
The first iota equals 0, and each time iota is used on a new line, its value is automatically incremented by 1.
The conditional control statement if & if elsepackage mainimport "fmt" func main () {var a = 12 if a > 10 {fmt.Println ("a > 10")} else {fmt.Println ("a% s\ n", k, v)} / / range can also be used to enumerate Unicode strings. The first parameter is the index of the character, and the second is the character (the value of Unicode) itself. For I, c: = range "go" {fmt.Println (I, c)}}
Running result
Sum: 9index: 1a-> appleb-> banana0 1031 111Map (collection)
Map is a collection of unordered key-value pairs. The most important thing about Map is to retrieve data quickly through key. Key is similar to an index and points to the value of the data.
Define the collection / * declaration variable. The default map is nil * / var map_variable map [key _ data_type] value_data_type/* using the make function * / map_variable: = make (map [key _ data_type] value_data_type)
If the map is not initialized, a nil map will be created. Nil map cannot be used to store key-value pairs
Package mainimport "fmt" func main () {var countryCapitalMap = make (map [string] string) / * map insert key-value pair Corresponding capitals of each country * / countryCapitalMap ["France"] = "Paris" countryCapitalMap ["Italy"] = "Rome" countryCapitalMap ["Japan"] = "Tokyo" countryCapitalMap ["India"] = "New Delhi" / * key output map value * / for country: = range countryCapitalMap {fmt.Println (country, "Capital is" CountryCapitalMap [country])} / * check whether the element exists in the collection * / capital, ok: = countryCapitalMap ["American"] / * if it is determined to be real, it exists, otherwise there is no * / * fmt.Println (capital) * / * fmt.Println (ok) * / if ok {fmt.Println ("the capital of American is" Capital)} else {fmt.Println ("the capital of American does not exist")} fmt.Println ("=") / / delete () function for country: = range countryCapitalMap {fmt.Println (country, "capital is", countryCapitalMap [country])} / / delete element delete (countryCapitalMap) "France") fmt.Println ("French entry deleted") for country: = range countryCapitalMap {fmt.Println (country, "Capital is", countryCapitalMap [country])}}
Running result
France Capital is Paris Italy Capital is Rome Japan Capital is Tokyo India Capital is New Delhi American Capital does not exist = India Capital is New Delhi France Capital is Paris Italy Capital is Rome Japan Capital is Tokyo French entry is deleted Japan Capital is Tokyo India Capital is New Delhi Italy Capital is Roman Recursive function
Recursion is to call yourself in the process of running
Factorial package mainimport "fmt" func main () {var i int = 15 fmt.Printf (the factorial of "% d is% d\ n", I, Factorial (uint64 (I))} func Factorial (n uint64) (result uint64) {if n > 0 {result = n * Factorial (NMAE 1) return result} return 1}
Running result
The factorial of 15 is 1307674368000 Fibonacci sequence package mainimport "fmt" func main () {var i int for i = 0; I < 10; iFunction + {fmt.Printf ("% d\ t", fibonacci (I))}} func fibonacci (n int) int {if n < 2 {return n} return fibonacci (NMAT 2) + fibonacci (NMAT 1)}
Running result
0 1 1 2 3 5 8 13 21 34 type conversion
Type conversion is used to convert a variable of one data type to another.
Type_name (expression) package mainimport "fmt" func main () {var sum int = 17 var count int = 5 var mean float32 mean = float32 (sum) / float32 (count) fmt.Printf ("mean value is:% f\ n", mean)}
Running result
The value of mean is: 3.400000 interface package mainimport "fmt" type Phone interface {call ()} type NokiaPhone struct {} type IPhone struct {} func main () {n: = new (NokiaPhone) n.call () I: = new (IPhone) i.call ()} func (NokiaPhone) call () {fmt.Println ("nokiaPhone")} func (IPhone) call () {fmt.Println ("IPhone")}
Running result
NokiaPhoneIPhone error handling
The Go language provides a very simple error handling mechanism through the built-in error interface.
The error type is an interface type, which is its definition
Type error interface {Error () string}
We can generate error messages in coding by implementing the error interface type.
The function usually returns an error message in the final return value. Use errors.New to return an error message
Package mainimport ("errors"fmt") func main () {_, err: = Sqrt (- 1) if err! = nil {fmt.Println (err)}} func Sqrt (f float64) (float64, error) {if f < 0 {return 0 Errors.New ("math: square root of negative number")} / / implement return f, nil}
Running result
Math: square root of negative number concurrency
The Go language supports concurrency, and we only need to turn on goroutine through the go keyword.
Goroutine is a lightweight thread, and the scheduling of goroutine is managed by the Golang runtime.
Goroutine syntax format:
Go function name (argument list)
Go allows you to start a new runtime thread, goroutine, using the go statement to execute a function with a different, newly created goroutine. All goroutine in the same program share the same address space.
Package mainimport ("fmt"time") func main () {go say ("world") say ("hello")} func say (s string) {for I: = 0; I < 5; iTunes + {time.Sleep (100 * time.Millisecond) fmt.Println (s)}}
Running result
Helloworldworldhellohelloworldworldhellohello Channel (channel)
A channel is a data structure used to transmit data.
The channel can be used to run and communicate synchronously between two goroutine by passing a value of a specified type. Operator
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.