g(x)=x odd and even expansions
We want to find a representation in terms of sines and cosines for the function
$g(x)$ which has value $x$ for $x \in [0,\pi]$. We don't really care if this
representation does not work well outside the interval $[0,\pi]$, but 
inside the interval, the representation better work perfectly.  This seems
to be crying out for a Fourier series approach, and so that's what we will
use.  The standard approach begins with functions that are periodic with
period $2\pi$ and so let us extend the definition of $g(x)$ to cover
one period $[0,2\pi]$.  


If we choose $g(x)$ to have value $2\pi-x$ for
$x \in [\pi, 2\pi]$, then, when repeated periodically,
this extended $g(x)$ is an even function of $x$ with average value $\pi/2$.
Furthermore, as is the case with all even functions, the sine terms all 
have $0$ coefficients.  Thus, the Fourier series works out to 
have only cosines in it, and the DC term is $\pi/2$.
If we choose $g(x)$ to have value $x$ for $x \in [\pi, 2\pi]$, then
when repeated periodically,
this extended $g(x)$ is not an odd function of $x$, but nonetheless
it is true that all the cosine terms in the Fourier series
turn out to be $0$ except the DC term which has value $\pi$.