Skip to content

Commit f32455b

Browse files
committed
identifiers in golang
1 parent ec8dffa commit f32455b

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

Fundamentels/identifiers.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main(){
6+
var name = "SiyaRaam"
7+
fmt.Println("Jai", name)
8+
}
9+
10+
/*
11+
There is a total of three identifiers available in the above example:
12+
- main: Name of the package
13+
- main: Name of the function
14+
- name: Name of the variable
15+
*/
16+
17+
/*
18+
Rules for Defining Identifiers:
19+
1. The name of the identifier must begin with a letter or an underscore(_). And the names may contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
20+
2. The name of the identifier should not start with a digit.
21+
3. The name of the identifier is case-sensitive.
22+
4. Keywords are not allowed to be used as an identifier name.
23+
5. There is no limit on the length of the name of the identifier, but it is advisable to use an optimum length of 4 – 15 letters only.
24+
*/
25+
26+
/*
27+
Example::::
28+
29+
// Valid identifiers:
30+
_geeks23
31+
geeks
32+
gek23sd
33+
Geeks
34+
geeKs
35+
geeks_geeks
36+
37+
// Invalid identifiers:
38+
212geeks
39+
if
40+
default
41+
42+
*/
43+
44+
/*
45+
In the Go language, there are some predeclared identifiers available for constants, types, and functions.
46+
These names are not reserved, you are allowed to use them in the declaration.
47+
Following is the list of predeclared identifiers:
48+
49+
For Constants:
50+
true, false, iota, nil
51+
52+
For Types:
53+
int, int8, int16, int32, int64, uint,
54+
uint8, uint16, uint32, uint64, uintptr,
55+
float32, float64, complex128, complex64,
56+
bool, byte, rune, string, error
57+
58+
For Functions:
59+
make, len, cap, new, append, copy, close,
60+
delete, complex, real, imag, panic, recover
61+
*/
62+
63+
/*
64+
-- The identifier represented by the underscore character(_) is known as a blank identifier. It is used as an anonymous placeholder instead of a regular identifier, and it has a special meaning in declarations, as an operand, and in assignments.
65+
-- The identifier which is allowed to access it from another package is known as the exported identifier. The exported identifiers are those identifiers which obey the following conditions:
66+
--- The first character of the exported identifier’s name should be in the Unicode upper case letter.
67+
--- The identifier should be declared in the package block or be a variable, function, type, or method name within that package.
68+
*/
69+
70+
/*
71+
72+
In the below example, file1.go contains an exported variable called ExportedVariable, which is accessible within the same file.
73+
It also imports the file2 package and accesses the exported variable AnotherExportedVariable from file2.go. By running go run file1.go, it will print the value of ExportedVariable (“Hello, World!”) from file1.go and the value of AnotherExportedVariable (“Greetings from file2!”) from file2.go.
74+
This demonstrates the concept of exported identifiers being accessible from another package in Go.
75+
*/
76+
77+
/*
78+
//file2.go
79+
80+
package file2
81+
82+
// Exported variable
83+
var AnotherExportedVariable = "Greetings from file2!"
84+
*/
85+
86+
/*
87+
// file1.go
88+
89+
package main
90+
91+
import (
92+
"fmt"
93+
"github.com/yourusername/project/file2"
94+
)
95+
96+
// Exported variable
97+
var ExportedVariable = "Hello, World!"
98+
99+
func main() {
100+
// Accessing exported identifier in the same file
101+
fmt.Println(ExportedVariable)
102+
103+
// Accessing exported identifier from another package
104+
fmt.Println(file2.AnotherExportedVariable)
105+
}
106+
*/
107+
108+
/*
109+
Output: (after running go run file1.go)
110+
111+
Hello, World!
112+
Greetings from file2!
113+
*/

0 commit comments

Comments
 (0)