Govur University Logo
--> --> --> -->
...

What are the basic syntax and data types in Go? Provide examples.



In Go, the basic syntax and data types play a fundamental role in defining variables, constants, and performing operations on them. Let's explore the basic syntax and data types in Go along with some examples:

1. Variables:

* Syntax: `var variableName dataType`
* Example:
```
go`var age int
age = 25`
```
2. Constants:

* Syntax: `const constantName dataType = value`
* Example:
```
go`const pi float64 = 3.14`
```
3. Numeric Data Types:

* `int`: Signed integers, can be of various sizes like `int8`, `int16`, `int32`, `int64`.
* `uint`: Unsigned integers, similar to `int`, but without negative values.
* `float32` and `float64`: Floating-point numbers.
* `complex64` and `complex128`: Complex numbers.
* Examples:
```
go`var count int = 10
var temperature float64 = 98.6
var c complex128 = complex(3, 4)`
```
4. Boolean Data Type:

* Syntax: `bool`
* Example:
```
go`var isFound bool = true`
```
5. String Data Type:

* Syntax: `string`
* Example:
```
go`var message string = "Hello, World!"`
```
6. Arrays:

* Syntax: `var arrayName [size]dataType`
* Example:
```
go`var numbers [5]int = [5]int{1, 2, 3, 4, 5}`
```
7. Slices:

* Syntax: `var sliceName []dataType`
* Example:
```
go`var colors []string = []string{"red", "green", "blue"}`
```
8. Maps:

* Syntax: `var mapName map[keyType]valueType`
* Example:
```
go`var ages map[string]int = map[string]int{
"John": 25,
"Alice": 30,
}`
```
9. Structs:

* Syntax: `type structName struct { field1 dataType1, field2 dataType2, ... }`
* Example:
```
go`type Person struct {
Name string
Age int
}
var p Person
p.Name = "John Doe"
p.Age = 35`
```
10. Pointers:

* Syntax: `var pointerName *dataType`
* Example:
```
go`var x int = 10
var pointerToX *int = &x`
```

These are some of the basic syntax and data types in Go. Understanding and utilizing these concepts allows developers to define variables, constants, and data structures, perform calculations, and manipulate data effectively in Go programs.