User Tools

Site Tools


programming:go

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:go [2023/09/04 19:35] skipidarprogramming:go [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:go to programming:go skipidar
Line 205: Line 205:
  
  
 +== Pointers ==
  
  
-== Structs ==+<sxh go> 
 +// basic-types/pointers/end/main.go 
 +package main 
 + 
 +import ( 
 + "fmt" 
 + "reflect" 
 +
 + 
 +func main() { 
 + // create a variable of type *T where T is an int 
 + var a *int 
 + 
 + // declare and assign `b` variable of type int 
 + valu := 100 
 + 
 + // assign the address of b to a 
 + pointr = &valu 
 + 
 + // print out the value of a which is the address of b 
 + fmt.Println(pointr) 
 + // returns: ptr 
 + fmt.Println("Type: %s", reflect.ValueOf(pointr).Kind() ) 
 + 
 + // print out the value at the address of b 
 + fmt.Println(*pointr) 
 +
 +</sxh> 
 + 
 + 
 +== Structs & pass by reference or value ==
  
 Structure to hold data in Go Lang Structure to hold data in Go Lang
 +
 +
  
  
Line 218: Line 251:
 } }
  
-// fullName returns the full name of the author+// getter  
 +// attention - the assignment to struct - is derived from parenthesis after "func" keyword 
 +// is declared OUTSIDE of struct :(
 func (a author) fullName() string { func (a author) fullName() string {
  return a.first + " " + a.last  return a.first + " " + a.last
 +}
 +
 +// setter
 +// passing the reference, so that we change the exact object and not its value
 +// changeName changes the first and last name of the author
 +func (a *author) changeName(first, last string) {
 + a.first = first
 + a.last = last
 } }
  
Line 235: Line 278:
  
 </sxh> </sxh>
 +
 +''*address'' - passes by reference
 +
 +Explains why to pass 
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/lzCAcBclf3.png?529x226}}
  
  
programming/go.1693856136.txt.gz · Last modified: by skipidar