When a Go program executes it executes a function named main Go statements don't require semicolons
()
| 287 | // When a Go program executes it executes a function named main |
| 288 | // Go statements don't require semicolons |
| 289 | func main() { |
| 290 | // Prints text and a newline |
| 291 | // List package name followed by a period and the function name |
| 292 | pl("Hello Go") |
| 293 | |
| 294 | // Get user input (To run this in the terminal go run hellogo.go) |
| 295 | pl("What is your name?") |
| 296 | // Setup buffered reader that gets text from the keyboard |
| 297 | reader := bufio.NewReader(os.Stdin) |
| 298 | // Copy text up to the newline |
| 299 | // The blank identifier _ will get err and ignore it (Bad Practice) |
| 300 | // name, _ := reader.ReadString('\n') |
| 301 | // It is better to handle it |
| 302 | name, err := reader.ReadString('\n') |
| 303 | if err == nil { |
| 304 | pl("Hello", name) |
| 305 | } else { |
| 306 | // Log this error |
| 307 | log.Fatal(err) |
| 308 | } |
| 309 | |
| 310 | // ----- VARIABLES ----- |
| 311 | // var name type |
| 312 | // Name must begin with letter and then letters or numbers |
| 313 | // If a variable, function or type starts with a capital letter |
| 314 | // it is considered exported and can be accessed outside the |
| 315 | // package and otherwise is available only in the current package |
| 316 | // Camal case is the default naming convention |
| 317 | |
| 318 | // var vName string = "Derek" |
| 319 | // var v1, v2 = 1.2, 3.4 |
| 320 | |
| 321 | // Short variable declaration (Type defined by data) |
| 322 | // var v3 = "Hello" |
| 323 | |
| 324 | // Variables are mutable by default (Value can change as long |
| 325 | // as the data type is the same) |
| 326 | // v1 := 2.4 |
| 327 | |
| 328 | // After declaring variables to assign values to them always use |
| 329 | // = there after. If you use := you'll create a new variable |
| 330 | |
| 331 | // ----- DATA TYPES ----- |
| 332 | // int, float64, bool, string, rune |
| 333 | // Default type 0, 0.0, false, "" |
| 334 | pl(reflect.TypeOf(25)) |
| 335 | pl(reflect.TypeOf(3.14)) |
| 336 | pl(reflect.TypeOf(true)) |
| 337 | pl(reflect.TypeOf("Hello")) |
| 338 | pl(reflect.TypeOf('🦍')) |
| 339 | |
| 340 | // ----- CASTING ----- |
| 341 | // To cast type the type to convert to with the variable to |
| 342 | // convert in parentheses |
| 343 | // Doesn't work with bools or strings |
| 344 | cV1 := 1.5 |
| 345 | cV2 := int(cV1) |
| 346 | pl(cV2) |
nothing calls this directly
no test coverage detected