forked from onecompiler/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfa34c8
commit 4c0c029
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
As the name suggests, data-type specifies the type of the data present in the variable. Variables must be declared with a data-type. | ||
|
||
## 1. Numeric Data types | ||
|
||
### Integer Data types | ||
| Data type | Description | Size|Range| | ||
|-----|-----|-----|----| | ||
|uint8|8-bit unsigned integer|1 byte|0 to 255| | ||
|int8|8-bit signed integer|1 byte|-128 to 127| | ||
|int16|16-bit signed integer|2 bytes|-32768 to 32767| | ||
|unit16|16-bit unsigned integer|2 bytes|0 to 65,535| | ||
|int32|32-bit signed integer|4 bytes|-2,147,483,648 to 2,147,483,647| | ||
|uint32|32-bit unsigned integer|4 bytes|0 to 4,294,967,295| | ||
|int64|64-bit signed integer|8 bytes| -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807| | ||
|uint64|64-bit unsigned integer|8 bytes|0 to 18,446,744,073,709,551,615| | ||
|
||
### Float Data types | ||
|
||
| Data type | Description | | ||
|-----|-----| | ||
|float32|32-bit signed floating point number| | ||
|float|64-bit signed floating point number| | ||
|complex32|Number has float32 real and imaginary parts| | ||
|complex64|Number has float32 real and imaginary parts| | ||
|
||
|
||
## 2. Boolean Data types | ||
|
||
| Data type | Description | Size|Range| | ||
|-----|-----|-----|----| | ||
|bool|Stores either true or false|1 byte|True or false| | ||
|
||
|
||
## 3. String Data types | ||
| Data type | Description | | ||
|-----|-----| | ||
|string|sequence of characters| | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/cmplx" | ||
) | ||
|
||
var ( | ||
integer uint32 = 1<<32 - 1 | ||
flt float64 = 3.14 | ||
complexNum complex128 = cmplx.Sqrt(8 - 6i) | ||
str string = "Hello World" | ||
boolean bool = true | ||
) | ||
|
||
func main() { | ||
fmt.Printf("Value: %v is of Type: %T\n", integer, integer) | ||
fmt.Printf("Value: %v is of Type: %T\n", flt, flt) | ||
fmt.Printf("Value: %v is of Type: %T\n", complexNum, complexNum) | ||
fmt.Printf("Value: %v is of Type: %T\n", str, str) | ||
fmt.Printf("Value: %v is of Type: %T\n", boolean, boolean) | ||
} | ||
``` | ||
|
||
### Check result [here](https://onecompiler.com/go/3vpkuw5dc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Variables are like containers which holds the data values. A variable specifies the name of the memory location. | ||
|
||
`var` statement is used to declares a list of variables with data type at last. You can declare variables either at package level or function level | ||
|
||
## How to declare variables | ||
|
||
```go | ||
var variable-name data-type; | ||
``` | ||
Mentioning datatype can be optional. | ||
|
||
## How to assign variables | ||
|
||
`:=` is used as short assignment and you no need to declare `var` as well. Please note you can use short assignment (`:=`) only inside functions. `:=` is not available outside functions. | ||
|
||
```go | ||
variable-name := value | ||
``` | ||
|
||
## Naming convention of variables | ||
|
||
* Variables names generally follow Camel case style. For example, firstName. Please note second word first letter is capital. | ||
* Variable name should follow either general English expression or shorthand. For example, employeeID can be given as `eid`. | ||
* It's good to start with bool variable names with `Has`, `Is`, `Can` or `Allow`, etc. | ||
* It is always advisable to give some meaningful names to variables. | ||
|
||
### Example | ||
|
||
```go | ||
|
||
package main | ||
|
||
import ("fmt") | ||
|
||
var str string = "Hello World" // declaring string variable at package level | ||
var boolean bool = true // declaring bool variable at package level | ||
var integer uint32 = 7979 // declaring uint32 variable at package level | ||
|
||
func main() { | ||
var i int // declaring int variable at functional level | ||
j := 99 // short assignment without var declaration | ||
fmt.Printf("Value: %v is of Type: %T\n", integer, integer) | ||
fmt.Printf("Value: %v is of Type: %T\n", i, i) | ||
fmt.Printf("Value: %v is of Type: %T\n", j, j) | ||
fmt.Printf("Value: %v is of Type: %T\n", str, str) | ||
fmt.Printf("Value: %v is of Type: %T\n", boolean, boolean) | ||
} | ||
``` | ||
|
||
### Check result [here](https://onecompiler.com/go/3vpkzg55z) |