Posts

Showing posts from January, 2022

24. Go - Bytes

 Byte strings are single quotes and have a type of byte. var byteone byte = '4'

23. Go - Creating Packages

1. Create folder "PACKAGENAME" 2. Create go file package PACKAGENAME import "fmt" func printname (a string ) {     fmt. Println (a) } 3. Make linkable object file go install 4. Import into another go as a package package main import m "UserData\VSCode\Go\test" func main () {     m. printname ( "hello" ) }

22. Go - Channels

 Channels allow goroutines to communicate with each other. Channels also allow buffering values to use later (pull). Make a channel: var channel1 chan string = make(chan string) // declare channel type and then the type that will be traded between this channel, which is string var channel1 chan string = make(chan string, 1) // 1 indicates that the channel has 1 buffer space. it can send/receive 1 msg until it is full channel1 := make(chan string) channel1 := make(chan string,1) // short declaration Sending a msg: channel1 <- "MESSAGE" // left is receiver channel // right is the message // channel sending blocks statements because it needs to send. // pulling from channel also blocks until it receives a value. Receiving a msg: msg := <- channel1 // variable is on left (receiver), channel1 value is on right(messenger) // statement will only run when there is a value waiting in the channel buffer. channel1 <- <- channel2 // Double arrows used when the channel2 is a s

21. Go - Goroutines Concurrency

 Concurrency, allows running something else while performing an action at the same time. Calling a Goroutine: go functionname(parameters)

20. Go - Panic and Recover

Panic("TEXT") // allows us to close the application and post an error Recover() // allows to recover from a panic, only if recover is in a defer before a panic

19. Go - Slices vs Array

Slices are arrays but without a fixed length (kind of, the array under it has a fixed length) Slices have a capacity and a length. Capacity is the fixed length of underlying array, which increases every time the slice's length above goes above array length. Old array gets dumped into new created array plus added value of slice to form a new slice. Slices and arrays are composite literals. Let's composite a group of values of the same type. Format: type{values}. Example: []int{1,2,3} Slice is a reference type meaning it will pass an address of its value instead of a copy. Slices hold pointers to an underlying array.      x := [][] int {[] int { 1 }, [] int { 1 , 2 }}     y := x     fmt. Println ( "Before appending 3 to Y[0]:" )     fmt. Println ( "X: " , x)     fmt. Println ( "Y: " , y)     y[ 0 ] = append (y[ 0 ], 3 )     fmt. Println ( "After appending 3 to Y[0]:" )     fmt. Println ( "X: " , x)     fmt. Println ( "Y: &q

18. Go - fmt Scan Inputs

 fmt.Scanln(&Variablename) // waits for input and gives the variable the input // does not consider whitespaces. only scans the first word that is before any whitespace fmt.Scanf()     var a string     var b int64     var c float32     var d bool     fmt. Scanf ( " %s %d %g %t " , &a, &b, &c, &d)     fmt. Println (a, b, c, d) // lets us format the scan. each whitespace in the input will be set to each variable separated in the format.

17. Go - Interfaces

Interfaces allow values to have/belong to more than one type. // "if you have these methods, you are my type" package main import "fmt" type species interface {     addage (a int ) } type human struct {     name string     age   int } type baboon struct {     name string     age   int } func (h *human) addage (a int ) {     h.age = h.age * a } func (b *baboon) addage (a int ) {     b.age = b.age * a } func main () {     peter := &human{         name: "peter" ,         age:   20 ,     }     bob := &baboon{         name: "ad" ,         age:   1 ,     }     peter. addage ( 5 )     bob. addage ( 5 )     list := []species{         peter,         bob,     }     fmt. Println (list[ 0 ]) } Interfaces is a type that lets you hold multiple structs that has exactly similar methods.  This allows easy storing of struct types. Methods must be exactly the same between structs. If the method has a pointer receiver for the struct, the v

23. Flutter - Android Studio Request Permissions

To request permissions edit the AndroidManifest.xml file. 1. Android>app>main>AndroidManifest.xml 2. Add line under manifest tree pertaining to specific permissions the app will use/request: <uses-permission android :name ="android.permission.WRITE_EXTERNAL_STORAGE" /> Use permission handler package for flutter: 1. Add "permission_handler: ^8.3.0" under dependencies in pubspec.yaml file 2. Import into a dart file: import 'package:permission_handler/permission_handler.dart'; 3. Create new function to request permission and/or check status of permission and handle it: permission () async { var status = await Permission. storage . status ; if (status. isDenied ) { // You can request multiple permissions at once. Map<Permission , PermissionStatus> statuses = await [ Permission. storage , ].request() ; print(statuses[Permission. storage ]) ; // it should print PermissionStatus.granted } }

22. Flutter - Http Package

Uses http to get data from third party online APIs. Install http package from: https://pub.dev/packages/http Edit pubspec.yaml file and import into a dart file. Getting data from an endpoint or place: get('ENDPOINT'); Using get inside a async function for effectiveness: function() async { Response response = get(Uri.http('ENDPOINT')); // EXAMPLE  Response response = await get( Uri . http ( "jsonplaceholder.typicode.com" , "todos/1" )) ; print(response.body); } // response type will make the variable whatever the get function gets from the endpoint and wraps in inside a body. // must separate sub domains from the url using a comma. Using built in convert package to decode json string: import 'dart:convert'; function() async {      Response response = get('ENDPOINT');      Map mapexample = jsonDecode(response.body); } // json strings will be converted to a map to use in dart.

21. Flutter - Packages/Libaries

Installing flutter packages from https://pub.dev/flutter/packages: 1. Edit pubspec.yaml file in android studio project 2. Add the instructed line under dependencies. 3. Import the package into a dart file to use.

12. Dart - Async & Await

Asynchronous functions run after a delay or timer had run. Creating a delayed async function: void function() { Future.delayed(Duration(seconds: #),(){}) } // Future has a delayed method that takes in two arguments. Duration and a function argument. The function runs after the duration. // Duration is a built in class in Dart. Using await type: void function() async{ await Future.delayed(Duration(seconds: #),(){}) } // Specifying the function as an async type and using await will tell function to only resume the next block of line only after the first await finishes. // can layer many types of await(s) under each other. Using await type as a variable to get return values: void function() async{      String text = await Future.delayed(Duration(seconds: #),(){           return "HELLO";      }) } // making the await function a variable lets us use it in other parts of the code