Continuous extension of a real function Related;
Open set in $\mathbb{R}$ is a union of at most countable collection of disjoint segments
This is the theorem i need to prove;
"Let $E(\subset \mathbb{R})$ be closed subset and $f:E\rightarrow \mathbb{R}$ be a contiuous function. Then there exists a continuous function $g:\mathbb{R} \rightarrow \mathbb{R}$ such that $g(x)=f(x), \forall x\in E$."
I have tried hours to prove this, but couldn't. I found some solutions, but ridiculously all are wrong. Every solution states that "If $x\in E$ and $x$ is not an interior point of $E$, then $x$ is an endpoint of a segment of at most countable collection of disjoint segments.". However, this is indeed false! (Check Arthur's argument in the link above)
Wrong solution Q4.5;
http://www.math.ust.hk/~majhu/Math203/Rudin/Homework15.pdf
Just like the argument in this solution, i can see that $g$ is continuous on $E^c$ and $Int(E)$. But how do i show that $g$ is continuous on $E$?
 A: A constructive and explicit proof proceeds as follows. Since $E$ is closed, $U=\mathbb{R}\setminus E$ is a countable union of disjoint open intervals, say, $U=\bigcup (a_n,b_n)$. Necessarily, we must have that $a_n,b_n\in E$. Define $f(x)$ as follows. 
$$ f(x) = 
\begin{cases}
g(x) &\text{if }x\in E \\
\frac{x-a_n}{b_n-a_n}g(b_n)+\frac{b_n-x}{b_n-a_n}g(a_n) & \text{if }x\in[a_n,b_n] 
\end{cases} $$
Notice first that $f(x)$ is well-defined and also, for all $x\in(a_n,b_n)$, either $g(a_n)\le f(x)\le g(b_n)$ or $g(b_n)\le f(x)\le g(a_n)$ depending on whether $g(a_n)\le g(b_n)$ or otherwise. Clearly, $f$ is continuous on $U$. Now suppose that $x\in E$ and $\epsilon>0$. Then there are a few cases. 
Case 1: Suppose that for every $\eta>0$, $(x-\eta,x)\cap E\not=\emptyset$ and $(x,x+\eta))\cap E\not=\emptyset$. Then since $f\vert_E=g$, there is some $\delta>0$ such that if $y\in E$ and $\vert x-y\vert<\delta$ then $\vert f(x)-f(y)\vert<\epsilon$. Because of the condition we have for Case 1, we may choose some $x_1,x_2\in E$ with $x-\delta<x_1<x<x_2<x+\delta$. Choose $\delta'=\min\{x-x_1,x_2-x\}$. If $\vert y-x\vert<\delta'$, then if $y\in E$, we're done. If $y\in U$, then $y\in(a_m,b_m)$ for some $m\in\mathbb{N}$. Furthermore, $a_m,b_m\in E$ and are within $\delta$ of $x$. Also, $f(y)$ is lies between $g(a_m)$ and $g(b_m)$. Thus $f(y)$ is within $\epsilon$ of $f(x)$ since $f(a_m)=g(a_m)$ and $f(b_m)=g(b_m)$ are within $\epsilon$ of $f(x)$. 
Case 2: There is some $\eta>0$ for which $(x-\eta,x)\cap E=\emptyset$ or $(x,x+\eta)\cap E=\emptyset$. In this case, $x$ is an endpoint of one of the intervals of $U$. Thus $f$ is linear on either $[x,x+\eta)$ or $(x-\eta,x]$ (maybe both). Certainly, we can get a $\delta>0$ corresponding to $\epsilon$ on this side of $x$. For the other side of $x$, use the argument from Case 1 to get some $\delta'$. Choosing $\delta''=\min\{\delta,\delta'\}$ proves the result. 
A: This is a special case of the Tietze extension theorem. This is a standard result whose proof can be found in any decent topology text. A rather different proof can be found here.
