You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added a more detailed explanation of how go code is organised, and fmt
package in Hello, World!.md
Added details about boolean data type and multiline strings in
Variables.md
Copy file name to clipboardExpand all lines: tutorials/learn-golang.org/en/Hello, World!.md
+38-1Lines changed: 38 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,44 @@
1
1
Tutorial
2
2
--------
3
3
4
-
Welcome to the first tutorial. In this tutorial you will learn how to write your first line of code.
4
+
Welcome to the first tutorial. This tutorial will guide you through your first go program.
5
+
6
+
Golang is a modern statistically typed and compiled language from Google.
7
+
8
+
In each golang program you need to define a package name at the top. So if you need to import your code into some other program you can use this package name to import. Execution of a golang project starts in the package `"main"`. So let's define our package as main.
9
+
10
+
package main
11
+
12
+
Go follows a similar pattern as C and has a main function where the execution begins. In go functions are defined using `func` keyword.
13
+
14
+
This is an example main function
15
+
16
+
func main () {
17
+
18
+
}
19
+
20
+
Now to print something we need to use the Println function from the package `"fmt"`
21
+
22
+
To import a function you just write import following the package name in "".
23
+
24
+
import "somemodule"
25
+
26
+
27
+
Here is an example program to print "Hello World!"
28
+
29
+
package main
30
+
31
+
import "fmt"
32
+
33
+
func main () {
34
+
fmt.Println("This line is printed")
35
+
}
36
+
37
+
We dont need to add semi colons after each line in Go. The compiler takes care of it.
38
+
39
+
The `fmt` package also has a `Printf()` function similar to printf in C. It can be used in the same way as it is used in C.
Copy file name to clipboardExpand all lines: tutorials/learn-golang.org/en/Variables.md
+27Lines changed: 27 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,10 @@ Tutorial
3
3
4
4
A variable is a name given to a storage area that the programs can manipulate. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore.
5
5
6
+
The general code to declare a variable in golang is
7
+
8
+
var <name of variable> <type of variable>
9
+
6
10
### Numbers
7
11
Variables are arithmetic types and represent the following values throughout the program.
8
12
a) integer types
@@ -33,6 +37,21 @@ Single quotes are not used to enable the use of apostrophes in strings without h
33
37
var s string = "Don't worry about apostrophes"
34
38
fmt.Println(s)
35
39
40
+
We can also define multiple line strings wrapping the string in `` quotes.
41
+
42
+
var s string = `This is a string spanning multiple lines
43
+
This is the second line
44
+
And this is the third`
45
+
46
+
fmt.Println(s)
47
+
48
+
### Booleans
49
+
Golang supports boolean values with the keywords `true` and `false`
50
+
51
+
Boolean variables are declared in go as follows
52
+
53
+
var b bool = true
54
+
36
55
### Shorthand Declaration
37
56
The `:=` notation serves both as a declaration and as initialization.
38
57
``` foo := "bar" ``` is equivalent to ``` var foo string = "bar" ```
@@ -44,6 +63,7 @@ The `:=` notation serves both as a declaration and as initialization.
44
63
e := "Hello"
45
64
f := `Do you like golang, so far?`
46
65
g := 'M'
66
+
h := true
47
67
48
68
fmt.Println(a)
49
69
fmt.Println(b)
@@ -52,6 +72,7 @@ The `:=` notation serves both as a declaration and as initialization.
52
72
fmt.Println(e)
53
73
fmt.Println(f)
54
74
fmt.Println(g)
75
+
fmt.Println(h)
55
76
56
77
57
78
@@ -63,6 +84,7 @@ You must print out to the console the following:
0 commit comments