몇 분만에 배우기
100244 단어 tutorialprogrammingbeginnersgo
소개
변수
// Global variables
var Integer int = 34
var Float32 float32 = 34.54
var isBoolean bool = true || false
var String string = ""
// Direct Declaration
func main() {
// this type of declaration only work inside functions. No Global values
item, price, quantity, isStock := "Mobile", 2000, 50, true
println(item, price+quantity, isStock)
}
// Group of Variables - These have a global Scope
var (
// access them inside a function
product string = "Mobile"
quantity int = 50
price float32 = 50.50
inStock bool = true || false
)
상수
상수는 나중에 변경할 수 없는 고정 값을 가진 상수입니다. 함수 내에서 사용할 수 없습니다. 전 세계적으로만 사용 가능합니다. 그러나 함수 내에서 액세스할 수 있습니다.
// Global Scope
const pricing int = 34
const name string = "Name"
const age float32 = 53.23
const isValue = true || false
// OR
const (
Pricing int = 34
Name string = "Name"
Age float32 = 53.23
IsValue = true || false
)
데이터 유형
Go는 정적으로 유형이 지정된 프로그래밍 언어입니다. 이는 변수가 항상 특정 유형을 가지며 해당 유형이 변경될 수 없음을 의미합니다. 키워드 var는 특정 데이터 유형의 변수를 선언하는 데 사용됩니다. 다음은 변수 선언 구문입니다.
var StringType string = "StringType"
// Int types can have negative values
var IntegerType int = 23
var IntegerType8 int8 = 43
var IntegerType16 int16 = -34
var IntegerType32 int32 = 345
var IntegerType64 int64 = -54
// uInt can't have negative values
var UintegerType uint = 23
var UintegerType8 uint8 = 43
var UintegerType16 uint16 = 34
var UintegerType32 uint32 = 345
var UintegerType64 uint64 = 54
// bool type
var isReallyBoolean = true || false
// float type
var FloatType32 float32 = 345.8
var FloatType64 float64 = -54.5
연산자
연산자는 컴파일러에게 특정 작업을 수행하도록 지시하는 기호입니다. 다음 목록은 Golang에서 사용되는 다양한 연산자를 설명합니다.
func arithmeticOperators() {
x := 5
y := 10
// Arithmetic Operators [+, -, *, /, %, ++ --]
// addition
println(x + y)
// subtraction
println(x - y)
// multiplication
println(x * y)
// division
println(x / y)
// modulus
println(x % y)
// incrimination
x++
println(x)
// decremention
y--
println(y)
}
func assignmentOperators() {
// Assignment Operators
var x, y = 15, 25
x = y
// assign
println("= ", x)
x = 15
x += y
// add and assign
println("+=", x)
x = 50
x -= y
// subtract and assign
println("-=", x)
x = 2
x *= y
// multiplication and assign
println("*=", x)
x = 100
x /= y
// divide and assign
println("/=", x)
x = 40
x %= y
// Divide and assign modulus
println("%=", x)
}
func comparisonOperators() {
// Comparison Operators
var x, y = 15, 25
// equality
println(x == y)
// not equal
println(x != y)
// less than
println(x < y)
// less than or equal
println(x <= y)
// greater than
println(x > y)
// greater than or equal
println(x >= y)
}
func logicalOperators() {
// Logical Operators
var x, y, z = 10, 20, 30
// && AND
println(x < y && x > z)
// || OR
println(x < y || x > z)
// isNot AND && OR ||
println(!(x == y && x > z))
}
func bitwiseOperators() {
var x uint = 9 //0000 1001
var y uint = 65 //0100 0001
var z uint
z = x & y
// AND Sets each bit to 1 if both bits are 1
println("x & y =", z)
z = x | y
// OR Sets each bit to 1 if one of two bits is 1
println("x | y =", z)
z = x ^ y
// XOR Sets each bit to 1 if only one of two bits is 1
println("x ^ y =", z)
z = x << 1
// Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
println("x << 1 =", z)
z = x >> 1
// Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
println("x >> 1 =", z)
}
조건문
func conditionalStatements() {
// if statement
var age int = 25
if true {
println(age)
}
if false {
println(false)
}
// if else statement
if age == 25 {
print(age)
} else {
println("Not Young Enough")
}
// if else - else if statement
if age == 2 {
println("Your age is Low")
} else if age == 25 {
println("Perfect")
} else {
println("Go Home Son")
}
// The if statement supports a composite syntax where the tested expression is
// preceded by an initialization statement.
if x := 10; x == 10 {
println("WOW: You are 10")
} else if x == 100 {
println("WOW: You are too OLD")
} else {
println("Congratulations")
}
}
스위치 케이스
// Switch with single case
func switchSingleCase() {
// to get the latest time
var today = time.Now()
// getting the todays date
switch today.Day() {
//if the date is 5
case 5:
println("Today is 5th of the month")
case 10:
println("Today is 10th of the month")
case 15:
println("Today is 15th of the month")
case 20:
println("Today is 20th of the month")
case 25:
println("Today is 25th of the month")
case 30:
println("Today is 30th of the month")
default:
println("Today Date is ", today)
}
}
// Switch with multiple cases
func switchWithMultipleCase() {
var today = time.Now()
switch today.Day() {
case 5, 10, 15:
println("Clean your house.")
case 25, 26, 27:
println("Buy some food.")
case 31:
println("Party tonight.")
default:
println("No information available for that day.")
}
}
func switchFallthroughCaseStatement() {
// The fallthrough keyword used to force the execution flow to fall through the successive case block.
today := time.Now()
switch today.Day() {
case 5:
println("Clean your house.")
fallthrough
case 10:
println("Buy some Soda.")
fallthrough
case 15:
println("Visit a doctor.")
fallthrough
case 25:
println("Buy some food.")
fallthrough
case 31:
println("Party tonight.")
default:
println("No information available for that day.")
}
}
func switchConditionalCasesStatement() {
today := time.Now()
switch {
case today.Day() < 5:
println("Clean your house.")
case today.Day() <= 10:
println("Buy some wine.")
case today.Day() > 15:
println("Visit a doctor.")
case today.Day() == 25:
println("Buy some food.")
default:
println("No information available for that day.")
}
}
func switchInitializerStatement() {
switch today := time.Now(); {
case today.Day() < 5:
println("Clean your house.")
case today.Day() <= 10:
println("Buy some wine.")
case today.Day() > 15:
println("Visit a doctor.")
case today.Day() == 25:
println("Buy some food.")
default:
println("No information available for that day.")
}
}
For 루프
Golang에는 for 루프가 단일 조건과 함께 사용될 때 동일한 목적을 제공하기 때문에 while 루프가 없습니다.
func forLoop() {
// traditional for-loop
for i := 0; i < 10; i++ {
println(i)
}
// for loop with single statement - just like while loop
var k int = 1
for k <= 10 {
k++
println(k)
}
// for loop with if statement
for i := 0; ; i++ {
println(k)
if k == 10 {
println("Reached 10")
}
}
// Infinity loop -- Go will automatically tells you unreachable CODE
v := 5
for {
println("Hello There")
if v == 10 {
break
}
v++
}
}
기능
선언은 func 키워드로 시작하고, 그 뒤에 원하는 함수 이름, 한 쌍의 괄호(), 함수 코드를 포함하는 블록이 옵니다.
다음 예제에는 이름이 SimpleFunction인 함수가 있습니다. 매개변수를 사용하지 않고 값을 반환하지 않습니다.
func add(x int, y int) {
total := 0
total = x + y
println(total)
}
// Function with int as return type
func addition(x int, y int) int {
total := 0
total = x + y
return total
}
// float return type
func adding(x float32, y float32) float32 {
return x - y
}
// string return type
func withString(a string, b string) string {
return a + b
}
// boolean return type
func isbooling(a bool, b bool) bool {
return a == b
}
// The return values of a function can be named in Golang
// Golang allows you to name the return values of a function.
// We can also name the return value by defining variables, here a variable total of
// integer type is defined in the function declaration for the value that the function returns.
func rectangle(length int, width int) (area int) {
var parameter int
parameter = 2 * (length + width)
println("Parameter: ", parameter)
area = length * width
return // Return statement without specify variable name
}
// Golang Functions Returning Multiple Values
// Functions in Golang can return multiple values,
// which is a helpful feature in many practical scenarios.
func square(length int, width int) (area int, parameter int) {
parameter = area + area + area + area
area = length * width
return // Return statement without specify variable name
}
// Passing Address to a Function
// Passing the address of variable to the function and the value of a variables
// modified using dereferencing inside body of function.
func update(a *int, t *string) {
*a = *a + 5 // deferring pointer address
*t = *t + " Doe" // deferring pointer address
return
}
func mainFunction() {
var age = 20
var text = "Small"
println("Before:", text, age)
update(&age, &text)
println("After :", text, age)
}
// Anonymous Functions
// An anonymous function is a function that was declared without any named identifier to refer to it.
// Anonymous functions can accept inputs and return outputs, just as standard functions do.
func anonymousFunctions() {
// Example - 3 Passing arguments to anonymous functions.
func(l int, b int) {
println(l * b)
}(20, 30)
}
// Example - 1
var (
area = func(l int, b int) int {
return l * b
}
)
// Example - 2 Function defined to accept a parameter and return value.
func an() {
println(
"100 (°F) = %.2f (°C)\n",
func(f float64) float64 {
return (f - 32.0) * (5.0 / 9.0)
}(100),
)
}
// High Order Function
// A Higher-Order function is a function that receives a function as an argument or returns the
// function as output.
//
// Higher order functions are functions that operate on other functions, either by
// taking them as arguments or by returning them.
func sum(x, y int) int {
return x + y
}
func partialSum(x int) func(int) int {
return func(y int) int {
return sum(x, y)
}
}
func highOrderFunction() {
partial := partialSum(3)
println(partial(7))
}
// Returning Functions from other Functions
func squareSum(x int) func(int) func(int) int {
return func(y int) func(int) int {
return func(z int) int {
return x*x + y*y + z*z
}
}
}
func returningFunction() {
println(squareSum(5)(6)(7))
}
// User Defined Function
// Golang also support to define our own function types.
// The modified version of above program with function types as below:
type First func(int) int
type Second func(int) First
func squareSums(x int) Second {
return func(y int) First {
return func(z int) int {
return x*x + y*y + z*z
}
}
}
배열
배열은 단일 유형의 요소 모음으로 구성된 데이터 구조이거나 단순히 특수 변수라고 말할 수 있습니다.
한 번에 둘 이상의 값을 보유할 수 있습니다. 배열이 보유하는 값은 다음과 같습니다.
요소 또는 항목이라고 합니다. 배열은 특정 수의 요소를 보유합니다.
성장하거나 축소될 수 없습니다. 서로 다른 데이터 유형을 요소로 처리할 수 있습니다.
Int, String, Boolean 등과 같은 배열. 의 첫 번째 요소 인덱스
배열의 모든 차원은 0이며 배열 차원의 두 번째 요소 인덱스입니다.
1 등입니다.
func arrays() {
// array that store the 5 int values
var intArray [5]int
// array that store the 5 string values
var strArray [5]string
strArray[0] = "United State America" // Assign a value to the first element
strArray[1] = "Canada" // Assign a value to the second element
strArray[2] = "Japan" // Assign a value to the third element
intArray[0] = 12 // Assign a value to the first element
intArray[1] = 12 // Assign a value to the second element
intArray[2] = 12 // Assign a value to the third element
println(strArray[0]) // Access the first element value
println(strArray[1]) // Access the second element value
println(strArray[2]) // Access the third element value
println(intArray[0]) // Access the first element value
println(intArray[1]) // Access the second element value
println(intArray[2]) // Access the third element value
// Initializing an Array with an Array Literal
// x can hold the only 5 values or less but not more than 5
x := [5]int{10, 20, 30, 40, 50} // Initialized with values
// in y if you print the other two value will automatically be zero
var y [5]int = [5]int{10, 20, 30} // Partial assignment
println(x)
println(y)
// Initializing an Array with ellipses...
// When we use ... instead of specifying the length. The compiler can identify the length of an array,
// based on the elements specified in the array declaration.
m := [...]int{10, 20, 30}
println(m) // list
println(len(m)) // length
// Initializing Values for Specific Elements
//When an array declare using an array literal, values can be initialize for specific elements.//
//A value of 10 is assigned to the second element (index 1) and a value of 30
//is assigned to the fourth element (index 3).
n := [5]int{1: 10, 3: 30}
println(n)
}
슬라이스
슬라이스는 데이터 컬렉션을 구현하고 관리하기 위한 유연하고 확장 가능한 데이터 구조입니다. 슬라이스는 모두 동일한 유형의 여러 요소로 구성됩니다. 슬라이스는 필요에 따라 확장 및 축소할 수 있는 동적 배열의 세그먼트입니다. 배열과 마찬가지로 슬라이스는 인덱스 가능하고 길이가 있습니다. 조각에는 용량 및 길이 속성이 있습니다.
func slicing() {
var intSlice []int
var strSlice []string
println(intSlice, strSlice)
// Declare Slice using Make
// Slice can be created using the built-in function make. When you use make,
// one option you have is to specify the length of the slice.
// When you just specify the length, the capacity of the slice is the same.
var intSliced = make([]int, 10) // when length and capacity is same
var strSliced = make([]string, 10, 20) // when length and capacity is different
println("intSlice \tLen: %v \tCap: %v\n", len(intSliced), cap(intSliced))
println("strSlice \tLen: %v \tCap: %v\n", len(strSliced), cap(strSliced))
// Initialize Slice with values using a Slice Literal
// A slice literal contain empty brackets followed by the type of elements the slice will hold,
// and a list of the initial values each element will have in curly braces.
var initializedIntSlice = []int{10, 20, 30, 40}
var initializedStrSlice = []string{"India", "Canada", "Japan"}
println("intSlice \tLen: %v \tCap: %v\n", len(initializedIntSlice), cap(initializedIntSlice))
println("strSlice \tLen: %v \tCap: %v\n", len(initializedStrSlice), cap(initializedStrSlice))
// Declare Slice using new Keyword
// A slice can be declare using new keyword followed by capacity
// in square brackets then type of elements the slice will hold.
var newIntSlice = new([50]int)[0:10]
println("intSlice \tLen: %v \tCap: %v\n", len(newIntSlice), cap(newIntSlice))
println(newIntSlice)
// Add Items
a := make([]int, 2, 5)
a[0] = 10
a[1] = 20
println("Slice A:", a)
println("Length is %d Capacity is %d\n", len(a), cap(a))
// using append keyword we can add values inside slice
a = append(a, 30, 40, 50, 60, 70, 80, 90)
println("Slice A after appending data:", a)
println("Length is %d Capacity is %d\n", len(a), cap(a))
// Access Items
var b = []int{10, 20, 30, 40}
// by the index we can access the values
println(b[0])
println(b[1])
println(b[0:4])
// Change Item Value
var c = []string{"America", "Canada", "Japan"}
println(c)
// based on index we can change the values
c[2] = "Germany"
println(c)
// Remove Item from Slice
var f = []string{"Pakistan", "Canada", "Japan", "Germany", "Italy"}
println(f)
f = RemoveIndex(strSlice, 3)
println(strSlice)
// Tricks of Slicing
var countries = []string{"pakistan", "japan", "canada", "australia", "russia"}
println("Countries: %v\n", countries)
println(":2 %v\n", countries[:2])
println("1:3 %v\n", countries[1:3])
println("2: %v\n", countries[2:])
println("2:5 %v\n", countries[2:5])
println("0:3 %v\n", countries[0:3])
println("Last element: %v\n", countries[4])
println("Last element: %v\n", countries[len(countries)-1])
println("Last element: %v\n", countries[4:])
println("All elements: %v\n", countries[0:])
println("Last two elements: %v\n", countries[3:])
println("Last two elements: %v\n", countries[len(countries)-2:])
println(countries[:])
println(countries[0:])
println(countries[0:])
// Append a slice to an existing slice
// The usage of triple-dot ... ellipsis used to append a slice.
var slice1 = []string{"america", "japan", "canada"}
var slice2 = []string{"australia", "russia"}
slice2 = append(slice2, slice1...)
// Checking if Item Exist
var g = []string{"America", "Canada", "Japan", "Germany", "Italy"}
println(itemExists(g, "Canada"))
println(itemExists(g, "Africa"))
}
// function to remove the Slice based on the INDEX
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
// function to check if item Exists in a slice
func itemExists(slice interface{}, item interface{}) bool {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("Invalid data-type")
}
for i := 0; i < s.Len(); i++ {
if s.Index(i).Interface() == item {
return true
}
}
return false
}
지도
지도는 데이터 요소에 대한 더 빠른 조회를 제공하는 해시 테이블을 사용하여 구현되며 키를 제공하여 값을 쉽게 검색할 수 있습니다. 맵은 정렬되지 않은 컬렉션이며 키/값 쌍이 반환되는 순서를 예측할 방법이 없습니다. 지도에 대한 모든 반복은 다른 순서를 반환할 수 있습니다.
func maps() {
// Map initialization
var employee = map[string]int{"Mark": 10, "Sandy": 20}
println(employee)
// empty map
var emptyMap = map[string]int{}
println(emptyMap)
// Map declaration using make function
var mapping = make(map[string]int)
mapping["Mark"] = 10
mapping["Sandy"] = 20
println(mapping)
employeeList := make(map[string]int)
employeeList["Mark"] = 10
employeeList["Sandy"] = 20
println(employeeList)
// Adding Items
var a = map[string]int{"Mark": 10, "Sandy": 20}
println(a) // Initial Map
a["Rocky"] = 30 // Add element
a["Josef"] = 40
println(a)
// Removing Element
var b = make(map[string]int)
b["Mark"] = 10
b["Sandy"] = 20
b["Rocky"] = 30
b["Josef"] = 40
println(b)
delete(b, "Mark")
println(b)
// Iterate over a Map
var c = map[string]int{"Mark": 10, "Sandy": 20,
"Rock": 30, "Brook": 40, "Stacy": 50}
for key, element := range c {
println("Key:", key, "=>", "Element:", element)
}
}
구조체
type Rectangle struct {
length float64
breadth float64
color string
}
/*
The rectangle struct and its fields are not exported to other packages because
identifiers are started with an lowercase letter. In Golang, identifiers are
exported to other packages if the name starts with an uppercase letter, otherwise
the accessibility will be limited within the package only.
*/
// Creating Instances of Struct Types
type Rectangular struct {
length int
breadth int
color string
// this is instance of the struct
geometry struct {
area int
perimeter int
}
}
// Creating a Struct Instance Using a Struct Literal
type Colors struct {
green string
blue string
black string
}
// Struct Instantiation Using Pointer Address Operator
type Countries struct {
firstCountry string
secondCountry string
thirdCountry string
}
// Nested Struct Type
// Struct can be nested by creating a Struct type using other Struct types as the type for
// the fields of Struct.
// Nesting one struct within another can be a useful way to model more complex structures.
type Salary struct {
Basic, HRA, TA float64
}
type Employee struct {
FirstName, LastName, Email string
Age int
// Upper Struct Inside a Struct
MonthlySalary []Salary
}
// Add Method to Struct Type
type Money struct {
Basic, HRA, TA float64
}
type Workers struct {
FirstName, LastName, Email string
Age int
MonthlySalary []Salary
}
func (e Workers) EmpInfo() string {
println(e.FirstName, e.LastName)
println(e.Age)
println(e.Email)
for _, info := range e.MonthlySalary {
println("===================")
println(info.Basic)
println(info.HRA)
println(info.TA)
}
return "----------------------"
}
func structures() {
println(Rectangle{10.5, 25.10, "red"})
// ---------------------------------------------
var rect Rectangular // dot notation
rect.length = 10
rect.breadth = 20
rect.color = "Green"
rect.geometry.area = rect.length * rect.breadth
rect.geometry.perimeter = 2 * (rect.length + rect.breadth)
println(rect)
println("Area:\t", rect.geometry.area)
println("Perimeter:", rect.geometry.perimeter)
// -----------------------------------------------
var firstColor = Colors{green: "Blue", blue: "Black", black: "Maroon"}
var secondColor = Colors{green: "Blue", blue: "Black", black: "Maroon"}
var thirdColor = Colors{green: "Blue", blue: "Black", black: "Maroon"}
println(firstColor, secondColor, thirdColor)
// ------------------------------------------------------
var firstCountries = &Countries{"America", "Pakistan", "Indonesia"}
var secondCountries = &Countries{}
secondCountries.firstCountry = "Australia"
secondCountries.secondCountry = "Iraq"
secondCountries.thirdCountry = "Labia"
println(firstCountries, secondCountries)
// -------------------------------------------------------
e := Employee{
FirstName: "Alisa",
LastName: "Jones",
Email: "[email protected]",
Age: 25,
MonthlySalary: []Salary{
{
Basic: 15000.00,
HRA: 5000.00,
TA: 2000.00,
},
{
Basic: 16000.00,
HRA: 5000.00,
TA: 2100.00,
},
{
Basic: 17000.00,
HRA: 5000.00,
TA: 2200.00,
},
},
}
println(e.FirstName, e.LastName)
println(e.Age)
println(e.Email)
println(e.MonthlySalary[0])
println(e.MonthlySalary[1])
println(e.MonthlySalary[2])
}
이제 당신은 Gopher입니다
GitHub 레포
Reference
이 문제에 관하여(몇 분만에 배우기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ayoubzulfiqar/learn-go-in-minutes-27h0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)