- Everything is organized as Packages.
- All files must have only 1 entrypoint. main() is the entrypoint and gets executed on its own.
- To initialize a go project. Run the following command
go mod init <folder name>
This creates a go.mod file. Which defines go version and the module name. 4. Main function doesn't accept any arguments. Neither returns anything. 5. Go package is a collection of source files. It gives various functionalities. Containers of various functionalities that go gives you for ready to use.
- If variable is constant then use
const
keyword before variable name. Else usevar
.
const conferenceTickets = 50
As number of available tickets is fixed i.e 50.
- To print formatted data. There is a function
printf
present infmt
package.%v
is the default. PS: For more details check printf documentation infmt
standard library.
-
If we are assigning the value to a variable and declaring the variable at the same time. Then, Go implicitly assigns the data type to variable based on the value.
-
If variable is only declared and no value is assigned. Then we need to explicitly mention the data type of variable.
var userName string
-
%T
is used to print type of the variable -
Syntatic Sugar in GO. Works only for variables and not constants.
Instead of writing
var conferenceName string = "Go Conference"
We can write it as
conferenceName := "Go Conference"
- Pointer is variable that points to memory address of another variable.
fmt.Scan(&userName)
- Array can contain only elements with same data type.
- Array size and type has to be declared in the beginning.
Below is the syntax to initialize empty array
var bookings[50]string{}
Syntax to initialize array with list of elements.
var bookings = [50]string{"Mridu", "Sakshi", "Shubh"}
- Dynamic arrays are slices. Slices are used when size is not fixed.
var bookings[]string
- To add an element to a slice. Use
append
. First argument to append is slice. Syntax.
bookings = append(bookings, firstName + " " + lastName)
No exit condition
for {
}
break
breaks out of the loop.continue
moves to next iteration without executing remaining part of the body.
- Can be accessed using square bracktes prints the ASCII value of the character
s := "Hello World"
fmt.Println(s[0])
- Use
%c
to print the character of string.
s := 'Hello World'
fmt.Printf("%c", s[0])
- Printing a part of the string.
// prints total 5 charaters. 0 to 4
fmt.Println(s[0:5])
fmt.Println(s[:5])
- Print string given only the starting index. Prints from W.
fmt.Println(s[6:])
- String concat works normally.
This would print
Hello World Again
s = s + " Again"
fmt.Println(s)
- String Literals
fmt.Println("Hello\nWorld")
fmt.Println("Hello\tWorld")
fmt.Println("Hello\bWorld")
- Built-in method
ToUpper
for converting string from lower case to uppercase.
s := "Hello World"
fmt.Println(strings.ToUpper(s))
- Built-in method
ToLower
for converting string from upper case to lower case.
fmt.Println(strings.ToLower(s))
- For check if string contains the prefix.
fmt.Println(strings.HasPrefix(s, "Hello"))
- For checking if string contains suffix
fmt.Println(strings.HasSuffix(s, "World))
- Replace. 1st argument - full string. 2nd argument - string you want to replace 3rd argument - new string you want to replace old string with 4th argument - If the string to be replaced appear more than once. Count of strings you want to replace.
fmt.Println(strings.Replace(s, "Hello", "World", 2))
- Learned to return error by solving collatz conjuncture exercism problem. Using
error
standard library.
import "error"
err := error.New("value of n cannot be given as 0")
- Used
math
standard library for calculating exponent to the base 10. Output returned is in float64. Type cast it based on your need.
import "math"
math.Pow(float64(10), float64(index1))
- Used library
strconv
for converting integer to string.
import strconv
strconv.Itoa(42)
os.ReadFile(<filepath>)
reads file all at once.os.Open(<filepath>)
opens the file.