C++ for Go Developers
Transfer Go experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.
Go and C++ side by side
The same program in Go and in C++, from the lessons of this course.
Strings and Output
Go strings are immutable byte sequences. C++ std::string is mutable and owns its character buffer. C++ std::string owns a mutable byte sequence; stream insertion formats values. Length counts bytes, not Unicode characters.
Go1package main2import ("fmt")3 4func main(){5fmt.Println(len("oak"))6}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}Text Equality
Go strings compare contents with ==. C++ std::string does too, while raw pointer equality compares addresses. C++ std::string equality compares contents. Raw pointer equality compares addresses; user classes need a deliberate equality operation.
Go1package main2import (3 "fmt"4)5 6func main() {7 a,b:="pen","pen"; fmt.Println(a==b)8}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}Fixed-size Arrays
Go arrays are values with length in their type, which resembles std::array more closely than a Go slice does. C++ std::array<T,N> is a fixed-size value type. Its size is part of its type, and normal copy duplicates its elements.
Go1package main2import (3 "fmt"4)5 6func main() {7 a:=[3]int{2,4,6}; fmt.Println(len(a))8}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}Value Copies
Go struct and array assignment copies values, while slice assignment shares backing storage. C++ vector assignment copies its owned elements. C++ vector and string copies own independent storage. Copying a raw pointer still shares the pointed-to object and…
Go1package main2import (3 "fmt"4)5 6func main() {7 a:=[]int{1,2}; b:=a; b[0]=9; fmt.Println(a[0])8}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15}Functions and Returns
Go functions can return multiple values. C++ uses a single return object, such as a pair or struct, for multiple results. C++ function declarations establish parameter and return types. A return value is not automatically printed.
Go1package main2import (3 "fmt"4)5func twice(n int) int { return n*2 }6func main() {7 fmt.Println(twice(4))8}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}Streams and Exact Parsing
Go parsers typically return a value and error. C++ streams encode success in stream state. C++ stream extraction reports success through stream state. Validate conversion and reject unexpected trailing tokens when the contract requires exact input.
Go1package main2import (3 "fmt"4 "strconv"5)6 7func main() {8 n,err:=strconv.Atoi("12"); if err!=nil { panic(err) }; fmt.Println(n)9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsC++ Files and Values
6 lessonsConditions and Iteration
5 lessonsStandard Containers
5 lessonsCopies and Algorithms
5 lessonsFunctions and Templates
5 lessonsParsing and Lifetime
5 lessonsObjects and Invariants
6 lessonsComposition and Ownership
5 lessonsProject: Library Desk
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is C++ for Go Developers for?
For people who already write Go and want to learn C++. Each lesson starts from Go you know and shows how C++ does the same thing, so you skip the beginner basics.
Do I need to know C++ already?
No. The course starts with your first C++ program. You need Go basics: variables, conditions, loops, functions and collections.
How many lessons does C++ for Go Developers have?
C++ for Go Developers contains 48 interactive lessons you can complete at your own pace.
What does C++ for Go Developers cover?
The course is organized into chapters covering: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, and more.
Is C++ for Go Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.