mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-14 18:55:00 +02:00
Updates
This commit is contained in:
@@ -9,18 +9,47 @@ commented code and explained as they go.
|
|||||||
... to write more inline code tutorials. Just grab an existing file from
|
... to write more inline code tutorials. Just grab an existing file from
|
||||||
this repo and copy the formatting (don't worry, it's all very simple).
|
this repo and copy the formatting (don't worry, it's all very simple).
|
||||||
Make a new file, send a pull request, and if it passes muster I'll get it up pronto.
|
Make a new file, send a pull request, and if it passes muster I'll get it up pronto.
|
||||||
Remember to fill in the author and author\_url fields so you get credited
|
Remember to fill in the "contributors" fields so you get credited
|
||||||
properly!
|
properly!
|
||||||
|
|
||||||
### Requests
|
### Requests
|
||||||
|
|
||||||
The most requested languages are:
|
We've had a ton of interest, b
|
||||||
|
|
||||||
* Go
|
### Contributing
|
||||||
* ~~Scala~~
|
|
||||||
* ~~Javascript~~
|
|
||||||
|
|
||||||
... but there are many more requests to do "every language", so don't let that stop you.
|
All contributions welcome, from the tiniest typo to a brand new article. Translations
|
||||||
|
in all languages are welcome (or, for that matter, original articles in any language).
|
||||||
|
|
||||||
|
#### Style Guidelines
|
||||||
|
|
||||||
|
* Try to keep **line length in code blocks to 80 characters or fewer**, or they'll overflow
|
||||||
|
and look odd.
|
||||||
|
|
||||||
|
* Try to use as few words as possible. Code examples are preferred over exposition in all cases.
|
||||||
|
|
||||||
|
* We welcome newcomers, but the target audience for this site is programmers with some experience.
|
||||||
|
So, try to avoid explaining basic concepts except for those specific to the language in question,
|
||||||
|
to keep articles succinct and scannable. We all know how to use google here.
|
||||||
|
|
||||||
|
* For translations (or english articles with non-ASCII characters), please make sure your file is
|
||||||
|
utf-8 encoded.
|
||||||
|
|
||||||
|
#### Header configuration
|
||||||
|
|
||||||
|
The actual site uses Middleman to generate HTML files from these markdown ones. Middleman, or at least
|
||||||
|
the custom scripts underpinning the site, required that some key information be defined in the header.
|
||||||
|
|
||||||
|
The following fields are necessary for english articles about programming languages:
|
||||||
|
|
||||||
|
* **language** The *programming language* in question
|
||||||
|
* **contributors** A list of [author, url] lists to credit
|
||||||
|
|
||||||
|
Other fields:
|
||||||
|
|
||||||
|
* **filename**: The filename for this article's code. It will be fetched, mashed together, and made downloadable.
|
||||||
|
For non-english articles, *filename* should have a language-specific suffix.
|
||||||
|
* **lang**: For translations, the human language this article is in. For categorization, mostly.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
language: bash
|
category: tool
|
||||||
|
tool: bash
|
||||||
contributors:
|
contributors:
|
||||||
- ["Max Yankov", "https://github.com/golergka" - "Darren Lin", "https://github.com/CogBear"]
|
- ["Max Yankov", "https://github.com/golergka"]
|
||||||
|
- ["Darren Lin", "https://github.com/CogBear"]
|
||||||
filename: LearnBash.sh
|
filename: LearnBash.sh
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -14,7 +16,8 @@ Nearly all examples below can be a part of a shell script or executed directly i
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# First line of the script is shebang which tells the system how to execute the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
|
# First line of the script is shebang which tells the system how to execute
|
||||||
|
# the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
|
||||||
# As you already figured, comments start with #. Shebang is also a comment.
|
# As you already figured, comments start with #. Shebang is also a comment.
|
||||||
|
|
||||||
# Simple hello world example:
|
# Simple hello world example:
|
||||||
@@ -27,12 +30,15 @@ echo 'This is the first line'; echo 'This is the second line'
|
|||||||
VARIABLE="Some string"
|
VARIABLE="Some string"
|
||||||
|
|
||||||
# But not like this:
|
# But not like this:
|
||||||
VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must execute and give an error because it couldn't be found.
|
VARIABLE = "Some string"
|
||||||
|
# Bash will decide that VARIABLE is a command it must execute and give an error
|
||||||
|
# because it couldn't be found.
|
||||||
|
|
||||||
# Using the variable:
|
# Using the variable:
|
||||||
echo $VARIABLE
|
echo $VARIABLE
|
||||||
echo "$VARIABLE"
|
echo "$VARIABLE"
|
||||||
# When you use the variable itself — assign it, export it, or else — you write it's name without $. If you want to use variable's value, you should use $.
|
# When you use the variable itself — assign it, export it, or else — you write
|
||||||
|
# its name without $. If you want to use variable's value, you should use $.
|
||||||
|
|
||||||
# Reading a value from input:
|
# Reading a value from input:
|
||||||
echo "What's your name?"
|
echo "What's your name?"
|
||||||
@@ -42,43 +48,46 @@ echo Hello, $NAME!
|
|||||||
# We have the usual if structure:
|
# We have the usual if structure:
|
||||||
if true
|
if true
|
||||||
then
|
then
|
||||||
echo "This is expected"
|
echo "This is expected"
|
||||||
else
|
else
|
||||||
echo "And this is not"
|
echo "And this is not"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Expressions are denoted with the following format:
|
# Expressions are denoted with the following format:
|
||||||
echo $(( 10 + 5 ))
|
echo $(( 10 + 5 ))
|
||||||
|
|
||||||
# Unlike other programming languages, bash is a shell — so it works in a context of current directory.
|
# Unlike other programming languages, bash is a shell — so it works in a context
|
||||||
# You can list files and directories in the current directories with ls command:
|
# of current directory. You can list files and directories in the current
|
||||||
|
# directories with ls command:
|
||||||
ls
|
ls
|
||||||
|
|
||||||
# These commands have options that control their execution:
|
# These commands have options that control their execution:
|
||||||
ls -l # Lists every file and directory on a separate line
|
ls -l # Lists every file and directory on a separate line
|
||||||
|
|
||||||
# Results of the previous command can be passed to the next command as input.
|
# Results of the previous command can be passed to the next command as input.
|
||||||
# grep command filters the input with provided patterns. That's how we can list txt files in the current directory:
|
# grep command filters the input with provided patterns. That's how we can list
|
||||||
|
# txt files in the current directory:
|
||||||
ls -l | grep "\.txt"
|
ls -l | grep "\.txt"
|
||||||
|
|
||||||
# Commands can be substitued within other commands using $( ):
|
# Commands can be substitued within other commands using $( ):
|
||||||
# The following command displays the number of files and directories in the current directory.
|
# The following command displays the number of files and directories in the
|
||||||
|
# current directory.
|
||||||
echo "There are $(ls | wc -l) items here."
|
echo "There are $(ls | wc -l) items here."
|
||||||
|
|
||||||
#Bash uses a case statement that works similarily to switch in Java and C++:
|
# Bash uses a case statement that works similarily to switch in Java and C++:
|
||||||
case "$VARIABLE"
|
case "$VARIABLE"
|
||||||
in
|
in
|
||||||
#List patterns for the conditions you want to meet
|
#List patterns for the conditions you want to meet
|
||||||
0) echo "There is a zero."
|
0) echo "There is a zero."
|
||||||
1) echo "There is a one."
|
1) echo "There is a one."
|
||||||
*) echo "It is not null."
|
*) echo "It is not null."
|
||||||
esac
|
esac
|
||||||
|
|
||||||
#For loops iterate for as many arguments given:
|
#For loops iterate for as many arguments given:
|
||||||
#The contents of var $VARIABLE is printed three times.
|
#The contents of var $VARIABLE is printed three times.
|
||||||
for $VARIABLE in x y z
|
for $VARIABLE in x y z
|
||||||
do
|
do
|
||||||
echo "$VARIABLE"
|
echo "$VARIABLE"
|
||||||
done
|
done
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@@ -112,14 +112,31 @@ println devMap.values()
|
|||||||
|
|
||||||
When Groovy is compiled to bytecode, the following rules are used.
|
When Groovy is compiled to bytecode, the following rules are used.
|
||||||
|
|
||||||
* If the name is declared with an access modifier (public, private or protected) then a field is generated.
|
* If the name is declared with an access modifier (public, private or
|
||||||
* A name declared with no access modifier generates a private field with public getter and setter (i.e. a property).
|
protected) then a field is generated.
|
||||||
* If a property is declared final the private field is created final and no setter is generated.
|
|
||||||
|
* A name declared with no access modifier generates a private field with
|
||||||
|
public getter and setter (i.e. a property).
|
||||||
|
|
||||||
|
* If a property is declared final the private field is created final and no
|
||||||
|
setter is generated.
|
||||||
|
|
||||||
* You can declare a property and also declare your own getter or setter.
|
* You can declare a property and also declare your own getter or setter.
|
||||||
* You can declare a property and a field of the same name, the property will use that field then.
|
|
||||||
* If you want a private or protected property you have to provide your own getter and setter which must be declared private or protected.
|
* You can declare a property and a field of the same name, the property will
|
||||||
* If you access a property from within the class the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going though the getter and setter.
|
use that field then.
|
||||||
* If you access a property that does not exist using the explicit or implicit foo, then Groovy will access the property through the meta class, which may fail at runtime.
|
|
||||||
|
* If you want a private or protected property you have to provide your own
|
||||||
|
getter and setter which must be declared private or protected.
|
||||||
|
|
||||||
|
* If you access a property from within the class the property is defined in
|
||||||
|
at compile time with implicit or explicit this (for example this.foo, or
|
||||||
|
simply foo), Groovy will access the field directly instead of going though
|
||||||
|
the getter and setter.
|
||||||
|
|
||||||
|
* If you access a property that does not exist using the explicit or
|
||||||
|
implicit foo, then Groovy will access the property through the meta class,
|
||||||
|
which may fail at runtime.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -185,13 +202,14 @@ for ( e in map ) {
|
|||||||
/*
|
/*
|
||||||
Operators
|
Operators
|
||||||
|
|
||||||
Operator Overloading for a list of the common operators that Groovy supports: http://groovy.codehaus.org/Operator+Overloading
|
Operator Overloading for a list of the common operators that Groovy supports:
|
||||||
|
http://groovy.codehaus.org/Operator+Overloading
|
||||||
|
|
||||||
Helpful groovy operators
|
Helpful groovy operators
|
||||||
*/
|
*/
|
||||||
//Spread operator: invoke an action on all items of an aggregate object.
|
//Spread operator: invoke an action on all items of an aggregate object.
|
||||||
def technologies = ['Groovy','Grails','Gradle']
|
def technologies = ['Groovy','Grails','Gradle']
|
||||||
technologies*.toUpperCase() //equivalent to: technologies.collect { it?.toUpperCase() }
|
technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() }
|
||||||
|
|
||||||
//Safe navigation operator: used to avoid a NullPointerException.
|
//Safe navigation operator: used to avoid a NullPointerException.
|
||||||
def user = User.get(1)
|
def user = User.get(1)
|
||||||
@@ -200,7 +218,8 @@ def username = user?.username
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Closures
|
Closures
|
||||||
A Groovy Closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point.
|
A Groovy Closure is like a "code block" or a method pointer. It is a piece of
|
||||||
|
code that is defined and then executed at a later point.
|
||||||
|
|
||||||
More info at: http://groovy.codehaus.org/Closures+-+Formal+Definition
|
More info at: http://groovy.codehaus.org/Closures+-+Formal+Definition
|
||||||
*/
|
*/
|
||||||
@@ -219,16 +238,13 @@ def x = 5
|
|||||||
def multiplyBy = { num -> num * x }
|
def multiplyBy = { num -> num * x }
|
||||||
println multiplyBy(10)
|
println multiplyBy(10)
|
||||||
|
|
||||||
//If you have a Closure that takes a single argument, you may omit the parameter definition of the Closure
|
// If you have a Closure that takes a single argument, you may omit the
|
||||||
|
// parameter definition of the Closure
|
||||||
def clos = { print it }
|
def clos = { print it }
|
||||||
clos( "hi" )
|
clos( "hi" )
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Groovy can memorize closure results:
|
Groovy can memorize closure results [1][2][3]
|
||||||
More info at:
|
|
||||||
http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
|
|
||||||
http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
|
|
||||||
http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html
|
|
||||||
*/
|
*/
|
||||||
def cl = {a, b ->
|
def cl = {a, b ->
|
||||||
sleep(3000) // simulate some time consuming processing
|
sleep(3000) // simulate some time consuming processing
|
||||||
@@ -256,9 +272,10 @@ callClosure(3, 4)
|
|||||||
/*
|
/*
|
||||||
Expando
|
Expando
|
||||||
|
|
||||||
The Expando class is a dynamic bean so we can add properties and we can add closures as methods to an instance of this class
|
The Expando class is a dynamic bean so we can add properties and we can add
|
||||||
|
closures as methods to an instance of this class
|
||||||
|
|
||||||
Reference: http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
|
http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
|
||||||
*/
|
*/
|
||||||
def user = new Expando(name:"Roberto")
|
def user = new Expando(name:"Roberto")
|
||||||
assert 'Roberto' == user.name
|
assert 'Roberto' == user.name
|
||||||
@@ -310,7 +327,8 @@ assertEquals "boo", f.boo
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
TypeChecked and CompileStatic
|
TypeChecked and CompileStatic
|
||||||
Groovy, by nature, is and will always be a dynamic language but it supports typechecked and compilestatic
|
Groovy, by nature, is and will always be a dynamic language but it supports
|
||||||
|
typechecked and compilestatic
|
||||||
|
|
||||||
More info: http://www.infoq.com/articles/new-groovy-20
|
More info: http://www.infoq.com/articles/new-groovy-20
|
||||||
*/
|
*/
|
||||||
@@ -373,7 +391,9 @@ Join a [Groovy user group](http://groovy.codehaus.org/User+Groups)
|
|||||||
|
|
||||||
* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)
|
* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)
|
||||||
|
|
||||||
|
[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
|
||||||
|
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
|
||||||
|
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,8 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: Go
|
|
||||||
category: language
|
|
||||||
language: Go
|
language: Go
|
||||||
filename: learngo.go
|
lang: hu-hu
|
||||||
|
filename: learngo-hu.go
|
||||||
contributors:
|
contributors:
|
||||||
- ["Sonia Keys", "https://github.com/soniakeys"]
|
- ["Sonia Keys", "https://github.com/soniakeys"]
|
||||||
translators:
|
translators:
|
||||||
@@ -20,276 +19,307 @@ a mai számítógépek több magos processzorait, ez nagy rendszerek építésé
|
|||||||
|
|
||||||
A Go alap könyvtára mindenre területre kiterjed, ennek köszönhetően a nyelvnek egyre növekvő tábora van.
|
A Go alap könyvtára mindenre területre kiterjed, ennek köszönhetően a nyelvnek egyre növekvő tábora van.
|
||||||
|
|
||||||
```Go
|
```go
|
||||||
// Egy soros komment
|
// Egy soros komment
|
||||||
/* Több
|
/* Több
|
||||||
soros komment */
|
soros komment */
|
||||||
|
|
||||||
// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python csomagkezelésére
|
// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python
|
||||||
// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz létre egy könyvtár helyett.
|
// csomagkezelésére
|
||||||
|
// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz
|
||||||
|
// létre egy könyvtár helyett.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a forrásfájlban
|
// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a
|
||||||
|
// forrásfájlban
|
||||||
import (
|
import (
|
||||||
"fmt" // A Go alap könyvtárának része
|
"fmt" // A Go alap könyvtárának része
|
||||||
"net/http" // Beépített webszerver!
|
"net/http" // Beépített webszerver!
|
||||||
"strconv" // Stringek átalakítására szolgáló csomag
|
"strconv" // Stringek átalakítására szolgáló csomag
|
||||||
)
|
)
|
||||||
|
|
||||||
// Funkció deklarás, a main nevű funkció a program kezdőpontja.
|
// Funkció deklarás, a main nevű funkció a program kezdőpontja.
|
||||||
func main() {
|
func main() {
|
||||||
// Println kiírja a beadott paramétereket a standard kimenetre.
|
// Println kiírja a beadott paramétereket a standard kimenetre.
|
||||||
// Ha más csomagot funkcióját akarjuk használni, akkor azt jelezni kell a csomag nevével
|
// Ha más csomagot funkcióját akarjuk használni, akkor azt jelezni kell a
|
||||||
fmt.Println("Hello world!")
|
// csomag nevével
|
||||||
|
fmt.Println("Hello world!")
|
||||||
|
|
||||||
// Meghívunk egy másik funkciót ebből a csomagból
|
// Meghívunk egy másik funkciót ebből a csomagból
|
||||||
beyondHello()
|
beyondHello()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A függvények paraméterei zárójelek között vannak.
|
// A függvények paraméterei zárójelek között vannak.
|
||||||
// Ha nincsenek paraméterek, akkor is kötelező a zárójel-pár.
|
// Ha nincsenek paraméterek, akkor is kötelező a zárójel-pár.
|
||||||
func beyondHello() {
|
func beyondHello() {
|
||||||
var x int // Változó deklaráció, használat előtt muszáj ezt megtenni.
|
var x int // Változó deklaráció, használat előtt muszáj ezt megtenni.
|
||||||
x = 3 // Változó értékadás
|
x = 3 // Változó értékadás
|
||||||
// "Rövid" deklaráció is létezik, ez az érték alapján deklarálja, definiálja és értéket is ad a változónak
|
// "Rövid" deklaráció is létezik, ez az érték alapján deklarálja,
|
||||||
y := 4
|
// definiálja és értéket is ad a változónak
|
||||||
sum, prod := learnMultiple(x, y) // a függvényeknek több visszatérési értéke is lehet
|
y := 4
|
||||||
fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás
|
sum, prod := learnMultiple(x, y) // a függvényeknek több
|
||||||
learnTypes()
|
// visszatérési értéke is lehet
|
||||||
|
fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás
|
||||||
|
learnTypes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A funkcióknak elnevezett visszatérési értékük is lehet
|
// A funkcióknak elnevezett visszatérési értékük is lehet
|
||||||
func learnMultiple(x, y int) (sum, prod int) {
|
func learnMultiple(x, y int) (sum, prod int) {
|
||||||
return x + y, x * y // visszatérünk két értékkel
|
return x + y, x * y // visszatérünk két értékkel
|
||||||
/*
|
/*
|
||||||
sum = x + y
|
sum = x + y
|
||||||
prod = x * y
|
prod = x * y
|
||||||
return
|
return
|
||||||
Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor.
|
Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor.
|
||||||
Üres return esetén, az elnevezett visszatérési változók
|
Üres return esetén, az elnevezett visszatérési változók
|
||||||
aktuális értékeikkel térnek vissza. */
|
aktuális értékeikkel térnek vissza. */
|
||||||
}
|
}
|
||||||
|
|
||||||
// Beépített típusok
|
// Beépített típusok
|
||||||
func learnTypes() {
|
func learnTypes() {
|
||||||
// Rövid deklarás az esetek többségében elég lesz a változókhoz
|
// Rövid deklarás az esetek többségében elég lesz a változókhoz
|
||||||
s := "Tanulj Go-t!" // string típus
|
s := "Tanulj Go-t!" // string típus
|
||||||
|
|
||||||
s2 := `A "nyers" stringekben lehetnek
|
s2 := `A "nyers" stringekben lehetnek
|
||||||
újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön típusa
|
újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön
|
||||||
|
// típusa
|
||||||
|
|
||||||
// nem ASCII karakterek. Minden Go forrás UTF-8 és a stringek is azok.
|
// nem ASCII karakterek. Minden Go forrás UTF-8 és a stringek is azok.
|
||||||
g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert tárol
|
g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert
|
||||||
|
// tárol
|
||||||
|
|
||||||
f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites lebegőpontos szám
|
f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites
|
||||||
c := 3 + 4i // complex128, belsőleg két float64-el tárolva
|
// lebegőpontos szám
|
||||||
|
c := 3 + 4i // complex128, belsőleg két float64-el tárolva
|
||||||
|
|
||||||
// Var szintaxis változó típus definiálással
|
// Var szintaxis változó típus definiálással
|
||||||
var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az int-nél
|
var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az
|
||||||
var pi float32 = 22. / 7
|
// int-nél
|
||||||
|
var pi float32 = 22. / 7
|
||||||
|
|
||||||
// Rövid deklarásnál átalakítás is lehetséges
|
// Rövid deklarásnál átalakítás is lehetséges
|
||||||
n := byte('\n') // byte típus, ami megegyezik az uint8-al
|
n := byte('\n') // byte típus, ami megegyezik az uint8-al
|
||||||
|
|
||||||
// A tömböknek fordítás-időben fixált méretük van
|
// A tömböknek fordítás-időben fixált méretük van
|
||||||
var a4 [4]int // egy tömb 4 int-tel, mind 0-ra inicializálva
|
var a4 [4]int // egy tömb 4 int-tel, mind 0-ra inicializálva
|
||||||
a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi értékekre
|
a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi
|
||||||
|
// értékekre
|
||||||
|
|
||||||
// Szeleteknek dinamikus a méretük. A szeleteknek és a tömböknek is meg vannak az előnyeik
|
// Szeleteknek dinamikus a méretük. A szeleteknek és a tömböknek is meg
|
||||||
// de a szeleteket sokkal gyakrabban használjuk.
|
// vannak az előnyeik de a szeleteket sokkal gyakrabban használjuk.
|
||||||
s3 := []int{4, 5, 9} // vesd össze a3-al, nincsenek pontok.
|
s3 := []int{4, 5, 9} // vesd össze a3-al, nincsenek pontok.
|
||||||
s4 := make([]int, 4) // allokál 4 int-et, mind 0-ra inicializálva
|
s4 := make([]int, 4) // allokál 4 int-et, mind 0-ra inicializálva
|
||||||
var d2 [][]float64 // ez csak deklaráció, semmi sincs még allokálva
|
var d2 [][]float64 // ez csak deklaráció, semmi sincs még allokálva
|
||||||
bs := []byte("a slice") // típus konverzió szintaxisa
|
bs := []byte("a slice") // típus konverzió szintaxisa
|
||||||
|
|
||||||
p, q := learnMemory() // deklarál két mutatót (p,q), két int-re
|
p, q := learnMemory() // deklarál két mutatót (p,q), két int-re
|
||||||
fmt.Println(*p, *q) // * követi a mutatót. Ez a sor kiírja a két int értékét.
|
fmt.Println(*p, *q) // * követi a mutatót. Ez a sor kiírja a két int értékét.
|
||||||
|
|
||||||
// A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít
|
// A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít
|
||||||
// a hash és dictionary típusokra más nyelvekben.
|
// a hash és dictionary típusokra más nyelvekben.
|
||||||
m := map[string]int{"three": 3, "four": 4}
|
m := map[string]int{"three": 3, "four": 4}
|
||||||
m["one"] = 1
|
m["one"] = 1
|
||||||
|
|
||||||
// A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban.
|
// A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban.
|
||||||
// Az aláhúzással "használod" a változókat, de eldobod az értéküket.
|
// Az aláhúzással "használod" a változókat, de eldobod az értéküket.
|
||||||
_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
|
_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
|
||||||
// Kiíratás is természetesen használatnak minősül
|
// Kiíratás is természetesen használatnak minősül
|
||||||
fmt.Println(s, c, a4, s3, d2, m)
|
fmt.Println(s, c, a4, s3, d2, m)
|
||||||
|
|
||||||
learnFlowControl()
|
learnFlowControl()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Go nyelv teljesen szemétgyűjtött (garbage-collected). Megtalálhatók benne mutatók, de nincs mutató aritmetika.
|
// A Go nyelv teljesen szemétgyűjtött (garbage-collected). Megtalálhatók benne
|
||||||
// Ez azt jelenti, hogy üres mutatóval még mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet.
|
// mutatók, de nincs mutató aritmetika. Ez azt jelenti, hogy üres mutatóval még
|
||||||
|
// mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet.
|
||||||
func learnMemory() (p, q *int) {
|
func learnMemory() (p, q *int) {
|
||||||
// Elnevezett visszatérési változóknak int-re mutató a típusa
|
// Elnevezett visszatérési változóknak int-re mutató a típusa
|
||||||
p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát allokál, és visszaad rá egy mutatót.
|
p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát
|
||||||
// Az allokált int nullázva van, p többé nem üres mutató.
|
// allokál, és visszaad rá egy mutatót.
|
||||||
s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen.
|
// Az allokált int nullázva van, p többé nem üres mutató.
|
||||||
s[3] = 7 // adjunk értéket az egyiknek
|
s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen.
|
||||||
r := -2 // hozzánk létre egy lokális változót
|
s[3] = 7 // adjunk értéket az egyiknek
|
||||||
return &s[3], &r // A & megadja a memóriacímét a változónak
|
r := -2 // hozzánk létre egy lokális változót
|
||||||
|
return &s[3], &r // A & megadja a memóriacímét a változónak
|
||||||
}
|
}
|
||||||
|
|
||||||
func expensiveComputation() int {
|
func expensiveComputation() int {
|
||||||
return 1e6
|
return 1e6
|
||||||
}
|
}
|
||||||
|
|
||||||
func learnFlowControl() {
|
func learnFlowControl() {
|
||||||
// Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges.
|
// Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges.
|
||||||
if true {
|
if true {
|
||||||
fmt.Println("megmondtam")
|
fmt.Println("megmondtam")
|
||||||
}
|
}
|
||||||
// A kód formátumát a nyelvvel járó "go" parancssori program "go fmt" parancsa szabványosítja
|
// A kód formátumát a nyelvvel járó "go" parancssori program "go fmt"
|
||||||
if false {
|
// parancsa szabványosítja
|
||||||
// így lehet
|
if false {
|
||||||
} else {
|
// így lehet
|
||||||
// if/else-t csinálni
|
} else {
|
||||||
}
|
// if/else-t csinálni
|
||||||
// Használjunk switchet a hosszabb elágazások alkalmazása helyett.
|
}
|
||||||
x := 1
|
// Használjunk switchet a hosszabb elágazások alkalmazása helyett.
|
||||||
switch x {
|
x := 1
|
||||||
case 0:
|
switch x {
|
||||||
case 1:
|
case 0:
|
||||||
// Az "esetek" nem "esnek át", tehát
|
case 1:
|
||||||
case 2:
|
// Az "esetek" nem "esnek át", tehát
|
||||||
// ez nem fog lefutni, nincs szükség break-ekre.
|
case 2:
|
||||||
}
|
// ez nem fog lefutni, nincs szükség break-ekre.
|
||||||
// A for ciklus sem használ zárójeleket
|
}
|
||||||
for x := 0; x < 3; x++ {
|
// A for ciklus sem használ zárójeleket
|
||||||
fmt.Println("iteráció", x)
|
for x := 0; x < 3; x++ {
|
||||||
}
|
fmt.Println("iteráció", x)
|
||||||
// itt az x == 1.
|
}
|
||||||
|
// itt az x == 1.
|
||||||
|
|
||||||
// A for az egyetlen ciklus fajta a Go-ban, de több formája van.
|
// A for az egyetlen ciklus fajta a Go-ban, de több formája van.
|
||||||
for { // végtelen ciklus
|
for { // végtelen ciklus
|
||||||
break // csak vicceltem
|
break // csak vicceltem
|
||||||
continue // soha nem fut le
|
continue // soha nem fut le
|
||||||
}
|
}
|
||||||
// Akárcsak a for-nál, az if-ben is lehet rövid deklarással egy lokális változót létrehozni
|
// Akárcsak a for-nál, az if-ben is lehet rövid deklarással egy lokális
|
||||||
// ami az blokk összes if/else-n keresztül érvényes marad.
|
// változót létrehozni ami az blokk összes if/else-n keresztül érvényes
|
||||||
if y := expensiveComputation(); y > x {
|
// marad.
|
||||||
x = y
|
if y := expensiveComputation(); y > x {
|
||||||
}
|
x = y
|
||||||
// Függvényeket használhatjuk closure-ként is.
|
}
|
||||||
xBig := func() bool {
|
// Függvényeket használhatjuk closure-ként is.
|
||||||
return x > 100 // a switch felett deklarált x-et használjuk itt
|
xBig := func() bool {
|
||||||
}
|
return x > 100 // a switch felett deklarált x-et használjuk itt
|
||||||
fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek)
|
}
|
||||||
x /= 1e5 // így most már x == 10
|
fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek)
|
||||||
fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis
|
x /= 1e5 // így most már x == 10
|
||||||
|
fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis
|
||||||
|
|
||||||
// Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t.
|
// Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t.
|
||||||
goto love
|
goto love
|
||||||
love:
|
love:
|
||||||
|
|
||||||
learnInterfaces() // Itt kezdődnek az érdekes dolgok!
|
learnInterfaces() // Itt kezdődnek az érdekes dolgok!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a String, ami visszatér egy stringgel.
|
// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a
|
||||||
|
// String, ami visszatér egy stringgel.
|
||||||
type Stringer interface {
|
type Stringer interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van, x és y.
|
// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van,
|
||||||
|
// x és y.
|
||||||
type pair struct {
|
type pair struct {
|
||||||
x, y int
|
x, y int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer interf
|
// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer
|
||||||
|
// interf
|
||||||
func (p pair) String() string { // p lesz a "vevő"
|
func (p pair) String() string { // p lesz a "vevő"
|
||||||
// Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s megfelelőjével.
|
// Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s
|
||||||
// A pontokkal érjük el a mindenkori p struktúra elemeit
|
// megfelelőjével. A pontokkal érjük el a mindenkori p struktúra elemeit
|
||||||
return fmt.Sprintf("(%d, %d)", p.x, p.y)
|
return fmt.Sprintf("(%d, %d)", p.x, p.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
func learnInterfaces() {
|
func learnInterfaces() {
|
||||||
// A kapcsos zárójellel jelezzük, hogy egyből inicializálni
|
// A kapcsos zárójellel jelezzük, hogy egyből inicializálni
|
||||||
// szeretnénk a struktúra változóit a sorrendnek megfelelően.
|
// szeretnénk a struktúra változóit a sorrendnek megfelelően.
|
||||||
p := pair{3, 4}
|
p := pair{3, 4}
|
||||||
fmt.Println(p.String()) // meghívjuk a p String metódusát.
|
fmt.Println(p.String()) // meghívjuk a p String metódusát.
|
||||||
var i Stringer // deklaráljuk i-t Stringer típusú interfésznek
|
var i Stringer // deklaráljuk i-t Stringer típusú interfésznek
|
||||||
i = p // lehetséges, mert a pair struktúra eleget tesz a Stringer interfésznek
|
i = p // lehetséges, mert a pair struktúra eleget tesz a
|
||||||
// Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb.
|
// Stringer interfésznek
|
||||||
fmt.Println(i.String())
|
// Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb.
|
||||||
|
fmt.Println(i.String())
|
||||||
|
|
||||||
// Az fmt csomag funckciói automatikusan meghívják a String funkciót
|
// Az fmt csomag funckciói automatikusan meghívják a String funkciót
|
||||||
// hogy megtudják egy objektum szöveges reprezentációját.
|
// hogy megtudják egy objektum szöveges reprezentációját.
|
||||||
fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja a String metódust.
|
fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja
|
||||||
fmt.Println(i) // dettó
|
// a String metódust.
|
||||||
|
fmt.Println(i) // dettó
|
||||||
|
|
||||||
learnErrorHandling()
|
learnErrorHandling()
|
||||||
}
|
}
|
||||||
|
|
||||||
func learnErrorHandling() {
|
func learnErrorHandling() {
|
||||||
// ", ok" szokásos megoldás arra, hogy jól működött-e a függvény.
|
// ", ok" szokásos megoldás arra, hogy jól működött-e a függvény.
|
||||||
m := map[int]string{3: "three", 4: "four"}
|
m := map[int]string{3: "three", 4: "four"}
|
||||||
if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban.
|
if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban.
|
||||||
fmt.Println("nincs meg")
|
fmt.Println("nincs meg")
|
||||||
} else {
|
} else {
|
||||||
fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban.
|
fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban.
|
||||||
}
|
}
|
||||||
// A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden "ok" volt-e
|
// A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden
|
||||||
if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket, úgy se lesz jó jelen esetben
|
// "ok" volt-e
|
||||||
// kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax"
|
if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket,
|
||||||
fmt.Println(err)
|
// úgy se lesz jó jelen
|
||||||
}
|
// esetben
|
||||||
// Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás!
|
// kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax"
|
||||||
learnConcurrency()
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
// Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás!
|
||||||
|
learnConcurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
// c egy csatorna, egy konkurens-biztos kommunikációs objektum.
|
// c egy csatorna, egy konkurens-biztos kommunikációs objektum.
|
||||||
func inc(i int, c chan int) {
|
func inc(i int, c chan int) {
|
||||||
c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így i+1-et küld be a csatornába
|
c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így
|
||||||
|
// i+1-et küld be a csatornába
|
||||||
}
|
}
|
||||||
|
|
||||||
// Az inc-et fogjuk arra használni, hogy konkurensen megnöveljünk számokat
|
// Az inc-et fogjuk arra használni, hogy konkurensen megnöveljünk számokat
|
||||||
func learnConcurrency() {
|
func learnConcurrency() {
|
||||||
// Ugyan az a make funkció amivel korábban szeleteket hoztunk létre.
|
// Ugyan az a make funkció amivel korábban szeleteket hoztunk létre.
|
||||||
// Make allokál mapokat, szeleteket és csatornákat.
|
// Make allokál mapokat, szeleteket és csatornákat.
|
||||||
c := make(chan int)
|
c := make(chan int)
|
||||||
// Indítsunk három konkurens goroutine-t. A számok konkurensen lesznek
|
// Indítsunk három konkurens goroutine-t. A számok konkurensen lesznek
|
||||||
// megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig paralellizálva/egymás mellett.
|
// megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig
|
||||||
// Mind a 3 ugyanabba a csatornába küldi az eredményeket.
|
// paralellizálva/egymás mellett. Mind a 3 ugyanabba a csatornába küldi az
|
||||||
go inc(0, c) // A go utasítás indít el goroutine-okat.
|
// eredményeket.
|
||||||
go inc(10, c)
|
go inc(0, c) // A go utasítás indít el goroutine-okat.
|
||||||
go inc(-805, c)
|
go inc(10, c)
|
||||||
// Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre.
|
go inc(-805, c)
|
||||||
// Nem lehet tudni milyen sorrendben fognak érkezni az eredmények!
|
// Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre.
|
||||||
fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a "<-" a beolvasó/kapó operátor
|
// Nem lehet tudni milyen sorrendben fognak érkezni az eredmények!
|
||||||
|
fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a
|
||||||
|
// "<-" a beolvasó/kapó operátor
|
||||||
|
|
||||||
cs := make(chan string) // még egy csatorna, ez stringekkel kommunikál
|
cs := make(chan string) // még egy csatorna, ez stringekkel kommunikál
|
||||||
cc := make(chan chan string) // egy csatorna csatornával
|
cc := make(chan chan string) // egy csatorna csatornával
|
||||||
go func() { c <- 84 }() // indítsunk egy új goroutine-t, csak azért hogy küldjünk egy számot
|
go func() { c <- 84 }() // indítsunk egy új goroutine-t, csak azért
|
||||||
go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet küldünk
|
// hogy küldjünk egy számot
|
||||||
// A select olyan mint a switch, csak feltételek helyett csatorna műveletek vannak.
|
go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet
|
||||||
// Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet kommunikáció.
|
// küldünk
|
||||||
select {
|
// A select olyan mint a switch, csak feltételek helyett csatorna műveletek
|
||||||
case i := <-c: // a megkapott értéket el lehet tárolni egy változóban
|
// vannak. Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet
|
||||||
fmt.Println("ez egy", i)
|
// kommunikáció.
|
||||||
case <-cs: // vagy el lehet dobni az értékét
|
select {
|
||||||
fmt.Println("ez egy string volt")
|
case i := <-c: // a megkapott értéket el lehet tárolni egy változóban
|
||||||
case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni
|
fmt.Println("ez egy", i)
|
||||||
fmt.Println("sose futok le :'( ")
|
case <-cs: // vagy el lehet dobni az értékét
|
||||||
}
|
fmt.Println("ez egy string volt")
|
||||||
// Ezen a ponton vagy c vagy a cs goroutineja lefutott.
|
case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni
|
||||||
// Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik blokkolva vár.
|
fmt.Println("sose futok le :'( ")
|
||||||
|
}
|
||||||
|
// Ezen a ponton vagy c vagy a cs goroutineja lefutott.
|
||||||
|
// Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik
|
||||||
|
// blokkolva vár.
|
||||||
|
|
||||||
learnWebProgramming() // Go képes rá. Te is képes akarsz rá lenni.
|
learnWebProgramming() // Go képes rá. Te is képes akarsz rá lenni.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Egy funkció a http csomagból elindít egy webszervert.
|
// Egy funkció a http csomagból elindít egy webszervert.
|
||||||
func learnWebProgramming() {
|
func learnWebProgramming() {
|
||||||
// A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd.
|
// A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd.
|
||||||
// Második paramétere egy interfész, pontosabban a http.Handler interfész.
|
// Második paramétere egy interfész, pontosabban a http.Handler interfész.
|
||||||
err := http.ListenAndServe(":8080", pair{})
|
err := http.ListenAndServe(":8080", pair{})
|
||||||
fmt.Println(err) // nem felejtük el kiírni az esetleges hibákat!
|
fmt.Println(err) // nem felejtük el kiírni az esetleges hibákat!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az egyetlen metódusát a ServeHTTP-t.
|
// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az
|
||||||
|
// egyetlen metódusát a ServeHTTP-t.
|
||||||
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Minden kapcsolatra elküldjük ezt a http.ResponseWriter-el
|
// Minden kapcsolatra elküldjük ezt a http.ResponseWriter-el
|
||||||
w.Write([]byte("Megtanultad a Go-t Y perc alatt!"))
|
w.Write([]byte("Megtanultad a Go-t Y perc alatt!"))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user