60 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			60 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								// For more tutorials: https://blog.learngoprogramming.com
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								// Copyright © 2018 Inanc Gumus
							 | 
						||
| 
								 | 
							
								// Learn Go Programming Course
							 | 
						||
| 
								 | 
							
								// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ---------------------------------------------------------
							 | 
						||
| 
								 | 
							
								// EXERCISE: Basics
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//  Let's warm up with the pointer basics. Please follow the
							 | 
						||
| 
								 | 
							
								//  instructions inside the code. Run the solution to see
							 | 
						||
| 
								 | 
							
								//  its output if you need to.
							 | 
						||
| 
								 | 
							
								// ---------------------------------------------------------
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								package main
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								type computer struct {
							 | 
						||
| 
								 | 
							
									brand string
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func main() {
							 | 
						||
| 
								 | 
							
									// create a nil pointer with the type of pointer to a computer
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// compare the pointer variable to a nil value, and say it's nil
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// create an apple computer by putting its address to a pointer variable
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// put the apple into a new pointer variable
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// compare the apples: if they are equal say so and print their addresses
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// create a sony computer by putting its address to a new pointer variable
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// compare apple to sony, if they are not equal say so and print their
							 | 
						||
| 
								 | 
							
									// addresses
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// put apple's value into a new ordinary variable
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// print apple pointer variable's address, and the address it points to
							 | 
						||
| 
								 | 
							
									// and, print the new variable's addresses as well
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// compare the value that is pointed by the apple and the new variable
							 | 
						||
| 
								 | 
							
									// if they are equal say so
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// print the values:
							 | 
						||
| 
								 | 
							
									// the value that is pointed by the apple and the new variable
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// create a new function: change
							 | 
						||
| 
								 | 
							
									// the func can change the given computer's brand to another brand
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// change sony's brand to hp using the func — print sony's brand
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// write a func that returns the value that is pointed by the given *computer
							 | 
						||
| 
								 | 
							
									// print the returned value
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// write a new constructor func that returns a pointer to a computer
							 | 
						||
| 
								 | 
							
									// and call the func 3 times and print the returned values' addresses
							 | 
						||
| 
								 | 
							
								}
							 |