COMP3007_Modern_Programming_Languages (7)
COMP3007_Modern_Programming_Languages (7)
Fall 2024-2025
1 Introduction to Go
2 Setting Up Go Environment
3 Basic Syntax
4 Functions
7 Conclusion
1 package main
2
3 import "fmt"
4
5 func main () {
6 fmt. Println ("Hello , World !")
7 }
1 var x int = 5
2 y := 10 // Short variable declaration
3
4 const PI = 3.14159
5
6 var (
7 name string = "John"
8 age int = 30
9 )
1 // Integer literals
2 x := 42 // decimal
3 y := 0x2A // hexadecimal
4 z := 052 // octal
5 b := 0b101010 // binary
6
7 // Floating -point literals
8 f1 := 3.14159
9 f2 := 1.23 e2 // scientific notation (123.0)
10 f3 := .5 // can omit leading zero
11
12 // String literals
13 s1 := "Hello\nWorld" // interpreted string literal
14 s2 := `Hello
15 World ` // raw string literal
16
17 // Character literals (rune)
18 c1 := 'A' // simple character
19 c2 := '\u0041 ' // Unicode code point
1
2 // Automatic type conversion in assignments is NOT allowed in Go
3 var i int32 = 100
4 var j int64 = i // Compile error!
5
6 // Automatic conversions in operations also not allowed
7 var f float64 = 3.14
8 var i int = 42
9 sum := f + i // Compile error!
10
11 // Constants have no type until used
12 const x = 100 // Untyped constant
13 var y int32 = x // OK: constant gets type int32
14 var z float64 = x // OK: constant gets type float64
Rules:
Must begin with letter or underscore
Can contain letters, numbers, underscores
Case-sensitive (user � User)
Exported names must start with capital letter
Length: 1 to any number of characters
Dr. Conventions:
Öğr. Üyesi Yusuf Kürşat Tuncel (Department
COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 16 / 23
Control Structures
1 if x > 0 {
2 fmt. Println (" Positive ")
3 } else if x < 0 {
4 fmt. Println (" Negative ")
5 } else {
6 fmt. Println ("Zero")
7 }
8 for i := 0; i < 5; i++ {
9 fmt. Println (i)
10 }
11 switch day {
12 case " Monday ":
13 fmt. Println (" Start of the week")
14 default :
15 fmt. Println (" Another day")
16 }
1 package main
2
3 import (
4 "fmt"
5 "math"
6 )
7
8 func main () {
9 fmt. Println ("The square root of 4 is", math.Sqrt (4))
10 }
1 // Array
2 var a [5] int
3
4 // Slice
5 s := [] int {1, 2, 3, 4, 5}
6 s = append (s, 6)