OLD | NEW |
1 <!-- Go For C++ Programmers --> | 1 <!-- Go For C++ Programmers --> |
2 | 2 |
3 <p> | 3 <p> |
4 Go is a systems programming language intended to be a general-purpose | 4 Go is a systems programming language intended to be a general-purpose |
5 systems language, like C++. | 5 systems language, like C++. |
6 These are some notes on Go for experienced C++ programmers. This | 6 These are some notes on Go for experienced C++ programmers. This |
7 document discusses the differences between Go and C++, and says little | 7 document discusses the differences between Go and C++, and says little |
8 to nothing about the similarities. | 8 to nothing about the similarities. |
9 | 9 |
10 <p> | 10 <p> |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 This is not precisely the same as a child class in C++. | 548 This is not precisely the same as a child class in C++. |
549 When a method of an anonymous field is called, | 549 When a method of an anonymous field is called, |
550 its receiver is the field, not the surrounding struct. | 550 its receiver is the field, not the surrounding struct. |
551 In other words, methods on anonymous fields are not virtual functions. | 551 In other words, methods on anonymous fields are not virtual functions. |
552 When you want the equivalent of a virtual function, use an interface. | 552 When you want the equivalent of a virtual function, use an interface. |
553 | 553 |
554 <p> | 554 <p> |
555 A variable which has an interface type may be converted to have a | 555 A variable which has an interface type may be converted to have a |
556 different interface type using a special construct called a type assertion. | 556 different interface type using a special construct called a type assertion. |
557 This is implemented dynamically | 557 This is implemented dynamically |
558 at runtime, like C++ <code>dynamic_cast</code>. Unlike | 558 at run time, like C++ <code>dynamic_cast</code>. Unlike |
559 <code>dynamic_cast</code>, there does | 559 <code>dynamic_cast</code>, there does |
560 not need to be any declared relationship between the two interfaces. | 560 not need to be any declared relationship between the two interfaces. |
561 | 561 |
562 <pre> | 562 <pre> |
563 type myPrintInterface interface { | 563 type myPrintInterface interface { |
564 print() | 564 print() |
565 } | 565 } |
566 func f3(x myInterface) { | 566 func f3(x myInterface) { |
567 x.(myPrintInterface).print() // type assertion to myPrintInterface | 567 x.(myPrintInterface).print() // type assertion to myPrintInterface |
568 } | 568 } |
(...skipping 13 matching lines...) Expand all Loading... |
582 <pre> | 582 <pre> |
583 type Any interface { } | 583 type Any interface { } |
584 </pre> | 584 </pre> |
585 | 585 |
586 <p> | 586 <p> |
587 Containers may be written in terms of <code>Any</code>, but the caller | 587 Containers may be written in terms of <code>Any</code>, but the caller |
588 must unbox using a type assertion to recover | 588 must unbox using a type assertion to recover |
589 values of the contained type. As the typing is dynamic rather | 589 values of the contained type. As the typing is dynamic rather |
590 than static, there is no equivalent of the way that a C++ template may | 590 than static, there is no equivalent of the way that a C++ template may |
591 inline the relevant operations. The operations are fully type-checked | 591 inline the relevant operations. The operations are fully type-checked |
592 at runtime, but all operations will involve a function call. | 592 at run time, but all operations will involve a function call. |
593 | 593 |
594 <pre> | 594 <pre> |
595 type iterator interface { | 595 type iterator interface { |
596 get() Any | 596 get() Any |
597 set(v Any) | 597 set(v Any) |
598 increment() | 598 increment() |
599 equal(arg *iterator) bool | 599 equal(arg *iterator) bool |
600 } | 600 } |
601 </pre> | 601 </pre> |
602 | 602 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 To use <code>manager2</code>, given a channel to it: | 698 To use <code>manager2</code>, given a channel to it: |
699 | 699 |
700 <pre> | 700 <pre> |
701 func f4(ch <- chan cmd2) int { | 701 func f4(ch <- chan cmd2) int { |
702 myCh := make(chan int) | 702 myCh := make(chan int) |
703 c := cmd2{ true, 0, myCh } // Composite literal syntax. | 703 c := cmd2{ true, 0, myCh } // Composite literal syntax. |
704 ch <- c | 704 ch <- c |
705 return <-myCh | 705 return <-myCh |
706 } | 706 } |
707 </pre> | 707 </pre> |
OLD | NEW |