본문 바로가기

go34

[Go] User-Defined Type Primitive Type based User-Defined Type type liters float64 type gallons float64 func main() { gasoline := liters(12.5) diesel := gallons(32.7) fmt.Println(gasoline, diesel) // 12.5 32.7 } You can create your own type based on primitive type. When converting a type, Go considers only the value itself. fmt.Println(gallons(liters(3.14))) // 3.14 So this example is not wrong, because they are all th.. 2020. 11. 2.
[Go] Package bufio Package bufio implements buffered I/O. Methods type Scanner 더보기 NewScanner NewScanner returns a new Scanner to read from r. func NewScanner (r io.Reader) *Scanner Err Err returns the first non-EOF error that was encountered by the Scanner. func (s *Scanner) Err() error Scan Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns fals.. 2020. 10. 31.
[Go] Package errors Package errors implements functions to manipulate errors. Methods New New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical. func New(text string) error Usages package main import ( "errors" "log" ) func main() { err := errors.New("error: my error") if err != nil { log.Fatal(err) } } // error: my error 2020. 10. 29.
[Go] Package math Package math Package math provides basic constants and mathematical functions. Package rand Package rand implements pseudo-random number generators. Methods Seed Seed uses the provided seed value to initialize the default Source to a deterministic state. Seed values that have the same remainder when divided by 2^31 - 1 generate the same pseudo-random sequence. Seed, unlike the Rand.Seed method, .. 2020. 10. 27.
[Go] Package strconv Package strconv implements conversions to and from string representations of basic data types. Methods Atoi Atoi equivalent to ParseInt(s, 10, 0), converted to type int. func Atoi(s string) (int, error) Itoa Itoa is equivalent to FormatInt. func Itoa(i int) string ParseBool, ParseUInt, ParseInt, ParseFloat ParseBool, ParseUInt, ParseInt, and ParseFloat convert strings to values. func ParseBool(s.. 2020. 10. 26.
[Go] Package strings Package strings implements simple functions to manipulate UTF-8 encoded strings. Methods TrimSpace TrimSpace returns a slice of the string, with all leading and trailing white space removed, as defined by Unicode. func TrimSpace(s string) string Usages package main import ( "fmt" "strings" ) func main() { greeting := "\n Hi, nice to meet you \t\n" fmt.Println(strings.TrimSpace(greeting)) // Hi, .. 2020. 10. 26.
[Go] Basic Go is a statically typed language. Let me show you a very default Go program. package main import "fmt" func main() { fmt.Println("Hello, Go!") } The first line declares which package to use. We run the program directly in the terminal, so the main package is needed. The next line imports fmt package. The example program prints the message through Println function in the fmt package. Then finall.. 2020. 10. 26.
[Go] Variable Variable is storage having a value. Variable Declaration You can give a name to a variable through variable declaration. var If you assign a value to a variable, use = symbol. You can also assign multiple values to multiple variables. var count int = 3 var name string = "HoYa" var width, height float64 = 2.6, 3.2 var count int = 5 // Error: variable can't redeclare. count = 7 name = 5 // Error: .. 2020. 10. 21.
[Go] Data Types As I already mentioned in the previous posting, Go is a statically typed language. So, the type of value is important. You can get the type of value. fmt.Println(reflect.TypeOf('a')) // int32 fmt.Println(reflect.TypeOf("Hello")) // string fmt.Println(reflect.TypeOf(true)) // bool fmt.Println(reflect.TypeOf(3)) // int fmt.Println(reflect.TypeOf(3.14)) // float64 Rune Rune represents a single char.. 2020. 10. 21.
[Go] Getting Started (시작하기) ※ 08/28/2020 - v1.15 Installation Official Homepage https://golang.org/dl You can download and install Go through this official website. You can also install Go with package managers. On MAC $ brew install go On Windows I am using Chocolatey. If you use this, you can install Go like this. $ choco install -y golang GOROOT On Windows GOROOT is an installation path of Go. Based on the current versi.. 2020. 8. 28.